【问题标题】:LINQ subquery with AND operator displays no results带有 AND 运算符的 LINQ 子查询不显示任何结果
【发布时间】:2009-02-11 09:00:23
【问题描述】:

又是我…… 我有一个 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 我会写类似的东西。

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

如何使用 linq 实现这一点? 我尝试了以下查询,但它返回 null。如果我将这两个属性与“或”||运算符我会得到结果,但如果一个项目同时符合两个条件,则会得到所有双重结果。

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();               

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

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

    (2009 年 12 月 2 日更新以排除空元素)

    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);
        }
    }
    

    【讨论】:

    • 非常感谢马克。现在可以了。不知何故,我无法用 isNullOrEmpty 弄清楚。
    【解决方案2】:

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

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

    这里是当前的扩展方法

        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;
    
        }
    

    【讨论】:

    • 顺便说一句 - 如果您使用 First,则不需要空值检查,因为如果找不到,它已经抛出异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多