LINQ subquery with AND operator does not display results

I again ... I have an XML file that contains different categories that I would like to request for different attributes.

        <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.

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 "OR" || I would get results, but with all duplicate results, if the element meets both criteria.

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();               
+3
source share
2 answers

I would simplify it with an extension method to do a complex search:

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

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);
    }
}
+3
source

, - , ".First" .

, , - , 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>

    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;

    }
0

Source: https://habr.com/ru/post/1703022/


All Articles