Using LINQ Group By to return new XElements
- by Jon
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)
)
);