views:

298

answers:

2

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)
                                          )
                             );
+1  A: 

x, per iteration, is the sequence of elements (excluding the first one). I suspect you want:

from grp in f
from x in grp
let sequenceNumber = x.Element("DOCUMENTID").Value
//...

Although you could also bring the group-key out in the projection if you wanted, by simplifying further:

    var f = (from x in MyDocument.Descendants("RECORD")
             where itemsThatWasDuplicated.Contains((int)x.Element("DOCUMENTID"))
             group x by x.Element("DOCUMENTID").Value);

    var errorQuery = (from grp in f
                      from x in grp.Skip(1)
                      let detail = "Sequence number " + grp.Key + " was read more than once"
    //...
Marc Gravell
A: 

I have managed to get this to compile and seems to work but maybe there are some performance tweaks needed or I'm doing something inefficient. What do you think?

errorQuery = MyDocument.Descendants("RECORD")
                        .Where(x => itemsThatWasDuplicated.Contains((int)x.Element("DOCUMENTID")))
                        .GroupBy(a => a.Element("DOCUMENTID"), (innerID, values) => values.OrderBy(b => b.Element("ID").Value))
                        .Skip(1)
                        .SelectMany(p => p)
                        .Select(item => new XElement("ERROR",
                                          new XElement("DATETIME", time),
                                          new XElement("DETAIL",  "Sequence number " + item.Element("DOCUMENTID").Value + " was read more than once"),
                                          new XAttribute("TYPE", "DUP"),
                                          new XElement("ID", item.Element("ID").Value)
                                          ));
Jon