簡體   English   中英

如何在LINQ中的select中添加where子句?

[英]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
};

我建議使用將SQL腳本轉換為LINQ的linquer軟件,在一次內部聯接和更多complecx查詢時想象一下嗎?

訪問: www.sqltolinq.com/

這是非常方便的工具!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM