![](/img/trans.png)
[英]How can I add a .Where into a .Select into an object using LINQ?
[英]How can I add in a where clause to a select in LINQ?
我有以下幾點:
var result =
from entry in feed.Descendants(a + "entry")
let content = entry.Element(a + "content")
let properties = content.Element(m + "properties")
let text = properties.Element(d + "Text")
let title = properties.Element(d + "Title")
let partitionKey = properties.Element(d + "PartitionKey")
select new Content
{
Text = text.Value,
Title = title.Value
};
我想做的是添加一個位置,以便僅將某些項目放入結果中。 如何添加條件:
partitionKey.Substring(2, 2) == "03" && text != null
選擇?
只需在選擇之前添加條件:
var result =
from entry in feed.Descendants(a + "entry")
let content = entry.Element(a + "content")
let properties = content.Element(m + "properties")
let text = properties.Element(d + "Text")
let title = properties.Element(d + "Title")
let partitionKey = properties.Element(d + "PartitionKey")
where partitionKey.Value.Substring(2, 2) == "03"
where text != null
select new Content
{
Text = text.Value,
Title = title.Value
};
代替
where partitionKey.Substring(2, 2) == "03" && text != null
采用
where partitionKey.Value.Substring(2, 2) == "03" && text != null
當需要它的值時,partitionKey的類型為XElement。
在select
之前指定where
:
var result =
from entry in feed.Descendants(a + "entry")
let content = entry.Element(a + "content")
let properties = content.Element(m + "properties")
let text = properties.Element(d + "Text")
let title = properties.Element(d + "Title")
let partitionKey = properties.Element(d + "PartitionKey")
where partitionKey.Substring(2, 2) == "03" && text != null
select new Content
{
Text = text.Value,
Title = title.Value
};
var result =
from entry in feed.Descendants(a + "entry")
let content = entry.Element(a + "content")
let properties = content.Element(m + "properties")
let text = properties.Element(d + "Text")
let title = properties.Element(d + "Title")
let partitionKey = properties.Element(d + "PartitionKey")
where partitionKey.Value.Substring(2, 2) == "03" && text != null
select new Content
{
Text = text.Value,
Title = title.Value
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.