C# 带有 AND 运算符的 LINQ 子查询不显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/535981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:10:47  来源:igfitidea点击:

LINQ subquery with AND operator displays no results

c#linqlinq-to-xml

提问by Chris

Me again... I have an XML File that contains different categories which I would like to query for different attributes.

我又来了...我有一个 XML 文件,其中包含我想查询不同属性的不同类别。

        <item>      
            <title>Industries</title>      
            <category type="Channel">Automotive</category>      
            <category type="Type">Cars</category>      
            <category type="Token">Article</category>      
            <category type="SpecialToken">News</category>      
            <guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>
        </item>

IN SQL I would write something like.

在 SQL 中,我会写一些类似的东西。

select * from articles where channel = 'Automative' AND type = 'Cars' etc. etc.

How can I achieve this with linq? I tried the following query but it returns null. If I combine the two attributes with the "OR" || operator I would get the results, but with all double results if an item matches both criteria.

我怎样才能用 linq 实现这一点?我尝试了以下查询,但它返回 null。如果我将这两个属性与“OR”相结合 || 运算符我会得到结果,但如果一个项目符合两个条件,所有结果都是双重的。

var articleList = (from item in doc.Descendants("item")

                         from _category in item.Elements("category")
                         where _category.Value == valueCboChannel && _category.Attribute("domain").Value == "Channel"
                         && (_category.Value == valueCboSubChannel && _category.Attribute("domain").Value == "SubChannel")

                         select new
                         {

                             Title = item.Element("title").Value,
                             Guid= item.Element("guid").Value,
                             description = item.Element("description").Value,
                             link = item.Element("link").Value
                         }).ToList();

            ListView1.DataSource = articleList;
            ListView1.DataBind();               

采纳答案by Marc Gravell

I'd simplify it with an extension method to do the tricky lookup:

我会用一个扩展方法来简化它来进行棘手的查找:

(updated 12/02/09 to exclude empty elements)

(更新 12/02/09 以排除空元素)

static string CategoryValue(this XElement element, string type)
{
    var category = element.Elements("category").FirstOrDefault(
        c => (string)c.Attribute("type") == type
            && !string.IsNullOrEmpty(c.Value)); // UPDATE HERE
    return category == null ? null : category.Value;
}

static void Main()
{
    XDocument doc = XDocument.Parse(xml);
    var qry = from item in doc.Descendants("item")
              where item.CategoryValue("Channel") == "Automotive"
              && item.CategoryValue("Type") == "Cars"
              select item;
    foreach (var node in qry)
    {
        Console.WriteLine(node.Element("guid").Value);
    }
}

回答by Chris

Thanks for the tip, I managed somehow to get it to work but only with ".First" in the extension method.

感谢您的提示,我设法以某种方式让它工作,但只能在扩展方法中使用“.First”。

The Problem I'm facing now is how to get all values where there are NULL values in the xml file. Basically the xml could look as follows. So if I use "First" then of course the first empty one will get selected, thus not displayed. Is there a way to skip the NULL values?

我现在面临的问题是如何获取 xml 文件中存在 NULL 值的所有值。基本上 xml 可能如下所示。因此,如果我使用“第一个”,那么当然会选择第一个空的,因此不会显示。有没有办法跳过 NULL 值?

<item>
    <title>Industries</title>
    <category type="Channel"></category> 
    <category type="Channel">Automotive</category> 
    <category type="Type"></category>     
    <category type="Type">Cars</category>     
    <category type="Token">Article</category>       
    <category type="SpecialToken">News</category>       
    <guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>  
</item>

Here the current extension method

这里是当前的扩展方法

    public static string CategoryValue(this XElement item, string type)
    {
        var category = item.Descendants("category").First(c => (string)c.Attribute("type") == type);
        return category == null ? null : category.Value;

    }