1

I have below xml.

<?xml version="1.0" encoding="utf-8" ?>
<MESSAGE ID="PND">
    <STORE ID="6697">
        <HEADER ID ="1" 
                LOADCLOSEDDATETIME="20130312121212" 
                DELIVERYDATE="20130312" 
                TRAILERID="" 
                TRIPROUTEID="" 
                DEPOTCODE=""/>
        <RECORD TPNB="123456666" 
                NOOFCASES="" 
                OUTERCASELENGTH="1" 
                OUTERCASEWIDTH="2" 
                OUTERCASEHEIGHT="3" 
                UNITSPERCASE="" 
                USEBYDATE="20130312121212" 
                MU="">
        </RECORD >
        <RECORD TPNB="123456666" 
                NOOFCASES="" 
                OUTERCASELENGTH ="1" 
                OUTERCASEWIDTH="2" 
                OUTERCASEHEIGHT="3" 
                UNITSPERCASE=""  
                USEBYDATE="20130312121212" 
                MU="">
        </RECORD>
    </STORE>
    <STORE ID="6647">
        <HEADER ID ="1" 
                LOADCLOSEDDATETIME="20130312121212"
                DELIVERYDATE="20130312" 
                TRAILERID="" 
                TRIPROUTEID="" 
                DEPOTCODE=""/>
        <RECORD TPNB="123456666" 
                NOOFCASES="" 
                OUTERCASELENGTH ="1" 
                OUTERCASEWIDTH="2"  
                OUTERCASEHEIGHT="3" 
                UNITSPERCASE="" 
                USEBYDATE="20130312121212" 
                MU="">
        </RECORD>        
    </STORE>
    <TRAILER ID="9" RECORDCOUNT=" 3" />
</MESSAGE>

I want to populate an entity with this xml where all my records belonging to a particular storeid should be grouped together into a list.

var _preNotifiedProduct = 
        from nlist in xDocument.Descendants("RECORD")
        group nlist by nlist.Anestors("STORE").Attributes().First().Value 
        into cust
        select new {key=cust.Key,value = ??};

Here key is my store id, and the value should be list of type (XElement) RECORD which belongs to that store id. If I assign value=cust this will show me all nonpublic member elements which are in the Record xelement list.

How do I get it into a xelement list?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Aditi Saha
  • 11
  • 2

1 Answers1

2

cust implements IEnumerable<XElement>, so you can just call ToList() on it:

var _preNotifiedProduct = 
              from nlist in xDocument.Descendants("RECORD")
              group nlist by nlist.Ancestors("STORE").Attributes().First().Value 
              into cust
              select new
              {
                  key = cust.Key,
                  value = cust.ToList()
              };

However, I would rewrite your query to become:

var _preNotifiedProduct = from store in xDocument.Root.Elements("STORE")
                          select new
                          {
                              key = (int)store.Attribute("ID"),
                              value = store.Elements("RECORD").ToList()
                          };

Should return the same, but should also be more readable and efficient.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263