locked
Nesten Linq query

    Question

  • Hi!

    I'm trying to Write a nested Linq Query, but there must be something I'm doing wrong for I can't get the colors. Can someone help me to get it right?

                List<Variant> variants = (from prod in xdoc.Root.Descendants("Item")
                                         select new Variant
                           {
                               product = prod.Element("No").Value,
                               name = prod.Element("Description").Value,
                               colors = (from col in prod.Element("Variants").Element("Variant").Value
                                         select new Color {
                                             Name = col.Value   
                                         }).ToList()
                           }).ToList();

    <Items>
    	<Item>
    		<No>610050</No>
    		<Description>Spyta Beanie</Description>
    		<Description_2>3</Description_2>
    		<Unit_Price>0</Unit_Price>
    		<Gross_weight>0</Gross_weight>
    		<Net_Weight>0</Net_Weight>
    		<Item_Category>29</Item_Category>
    		<Inventory>3</Inventory>
    		<Variants>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>CBLUE-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>0</Variant_Quantity>
    			</Variant>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>EBONY-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>3</Variant_Quantity>
    			</Variant>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>FPINK-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>0</Variant_Quantity>
    			</Variant>
    </Variants>
    
    </Item>
    </Items>

    Monday, May 05, 2014 8:33 PM

Answers

  • When you declare prod.Element("Variants").Element("Variant").Value, you are getting a concatenated string of the first <Variant> node "610050CBLUE-OZSpyta Beanie0", so the from col is just looking at the 27 characters in the string, and putting each character as the Value.

    You should instead use a nested query like this:

    List<Variant> variants = (from prod in xdoc.Root.Descendants("Item")
                                          select new Variant
                                          {
                                              product = prod.Element("No").Value,
                                              name = prod.Element("Description").Value,
                                              colors = (from vars in prod.Descendants("Variants")
                                                            from var in vars.Descendants("Variant")
                                                                select new Color 
                                                                {
                                                                    Name = var.Element("Variant_Code").Value
                                                                }                                                        
                                                            ).ToList()}).ToList();


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Sigurd F Friday, May 09, 2014 6:42 AM
    Monday, May 05, 2014 9:49 PM
    Moderator

All replies

  • When you declare prod.Element("Variants").Element("Variant").Value, you are getting a concatenated string of the first <Variant> node "610050CBLUE-OZSpyta Beanie0", so the from col is just looking at the 27 characters in the string, and putting each character as the Value.

    You should instead use a nested query like this:

    List<Variant> variants = (from prod in xdoc.Root.Descendants("Item")
                                          select new Variant
                                          {
                                              product = prod.Element("No").Value,
                                              name = prod.Element("Description").Value,
                                              colors = (from vars in prod.Descendants("Variants")
                                                            from var in vars.Descendants("Variant")
                                                                select new Color 
                                                                {
                                                                    Name = var.Element("Variant_Code").Value
                                                                }                                                        
                                                            ).ToList()}).ToList();


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Sigurd F Friday, May 09, 2014 6:42 AM
    Monday, May 05, 2014 9:49 PM
    Moderator
  • If you want to get every variant as a list in one object, check this solution

    XDocument xdoc = XDocument.Parse(@"<Items>
    	<Item>
    		<No>610050</No>
    		<Description>Spyta Beanie</Description>
    		<Description_2>3</Description_2>
    		<Unit_Price>0</Unit_Price>
    		<Gross_weight>0</Gross_weight>
    		<Net_Weight>0</Net_Weight>
    		<Item_Category>29</Item_Category>
    		<Inventory>3</Inventory>
    		<Variants>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>CBLUE-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>0</Variant_Quantity>
    			</Variant>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>EBONY-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>3</Variant_Quantity>
    			</Variant>
    			<Variant>
    				<Variant_No.>610050</Variant_No.>
    				<Variant_Code>FPINK-OZ</Variant_Code>
    				<Variant_Description>Spyta Beanie</Variant_Description>
    				<Variant_Quantity>0</Variant_Quantity>
    			</Variant>
    </Variants>
    
    </Item>
    </Items>");
    
     var variants = (from prod in xdoc.Root.Descendants("Item")
                                select new
                                {
                                    product = prod.Element("No").Value,
                                    name = prod.Element("Description").Value,
                                    colors =
                                                  (
                                              from col in prod.Element("Variants").Elements()
                                              select new
                                              {
                                                  y =
                                                  (from x in col.Elements()
                                                   select new
                                      {
                                          Name = x.Value
                                      }).ToList()
                                              })
                                }).ToList();
    

    and if you want to get all variants in one list check this one.

     var variants = (from prod in xdoc.Root.Descendants("Item")
                                select new
                                {
                                    product = prod.Element("No").Value,
                                    name = prod.Element("Description").Value,
                                    colors = (from x in
                                                  (from col in prod.Element("Variants").Elements()
                                                   select col).Elements()
    
    
                                              select new
                                              {
                                                  Name = x.Value
                                              }).ToList()
    
                                }).ToList();

    By the way I removed Variant class so I can test the sample.

    Regards,


    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Monday, May 05, 2014 10:07 PM
  • Thanks for the help :-)

    Regards, Sigurd F

    Friday, May 09, 2014 6:42 AM
  • Thanks for the help :-)

    Regards, Sigurd F

    Friday, May 09, 2014 6:43 AM