Home > front end >  using a variable in a Linq where filter && an attribute
using a variable in a Linq where filter && an attribute

Time:10-06

Hi I want to filter on an element and an attribute just like in the MS Example but instead of using a strict string I would like to use a variable (element from an Array or list, like state in the example below. Is this possible?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LinqFilterTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            XElement root = XElement.Load(@"C:\Users\john\Desktop\PurchaseOrdersInNamespace.xml");
            XNamespace aw = "http://www.adventure-works.com";

            string[] items = new string[3]
            {
               "NY" , "CA", "FL"

            };


            for (int i = 0; i < 3; i  )
            {
                var state = items[i];
                IEnumerable<XElement> purchaseOrders =
                    from el in root.Elements(aw   "PurchaseOrder")
                    where
                        (from add in el.Elements(aw   "Address")
                         where
                             //(string)add.Attribute(aw   "Type") == "Shipping" &&
                             (string)add.Element(aw   "State") == state
                         select add)
                        .Any()
                    select el;
                foreach (XElement el in purchaseOrders)
                    Console.WriteLine((string)el.Attribute(aw   "State"));

            }
        }
    }
}

CodePudding user response:

Edited: works fine with variable.

 static void Main(string[] args)
 {
     XElement root = default;
     string[] items = new string[3] { "NY", "CA", "FL" };

     // Without Namespace
     Console.WriteLine("Without namespace LINQ:\n");
     root = XElement.Load("H:\\PurchaseOrders.xml");

     foreach (var el in from state in items
                        let purchaseOrders = from el in root.Elements("PurchaseOrder")
                                             where (from add in el.Elements("Address")
                                                    where (string)add.Attribute("Type") == "Shipping"
                                                       && (string)add.Element("State") == state
                                                    select add).Any()
                                             select el
                        from XElement el in purchaseOrders
                        select el)
     {
         Console.WriteLine((string)el.Attribute("PurchaseOrderNumber"));
     }

     // With Namespace
     Console.WriteLine("\nWith namespace LINQ:\n");
     root = XElement.Load("H:\\PurchaseOrdersInNamespace.xml");
     XNamespace aw = "http://www.adventure-works.com";

     foreach (var el in from state in items
                        let purchaseOrders = from el in root.Elements(aw   "PurchaseOrder")
                                             where (from add in el.Elements(aw   "Address")
                                                    where (string)add.Attribute(aw   "Type") == "Shipping" 
                                                       && (string)add.Element(aw   "State") == state
                                                    select add).Any()
                                             select el
                        from XElement el in purchaseOrders
                        select el)
     {
         Console.WriteLine((string)el.Attribute(aw   "PurchaseOrderNumber"));
     }

     Console.ReadKey();
}

enter image description here

  • Related