Using LINQ Group By to return new XElements

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-03-30T15:38:29Z Indexed on 2010/03/30 15:43 UTC
Read the original article Hit count: 487

Filed under:
|
|
|
|

I have the following code and got myself confused:

I have a query that returns a set of records that have been identified as duplicates and I then want to create a XElement for each one. This should be done in one query I think but I'm now lost.

var f = (from x in MyDocument.Descendants("RECORD")
                              where itemsThatWasDuplicated.Contains((int)x.Element("DOCUMENTID"))
                              group x by x.Element("DOCUMENTID").Value into g
                              let item = g.Skip(1)  //Ignore first as that is the valid one
                              select item
                              );

var errorQuery = (from x in f 
                              let sequenceNumber = x.Element("DOCUMENTID").Value
                              let detail = "Sequence number " + sequenceNumber + " was read more than once"
                              select new XElement("ERROR",
                                          new XElement("DATETIME", time),
                                          new XElement("DETAIL", detail),
                                          new XAttribute("TYPE", "DUP"),
                                          new XElement("ID", x.Element("ID").Value)
                                          )
                             );

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0