使用 LINQ Group By 返回新的 XElement

发布于 2024-08-26 23:13:55 字数 1182 浏览 5 评论 0原文

我有以下代码,但让自己感到困惑:

我有一个查询返回一组已被标识为重复项的记录,然后我想为每个记录创建一个 XElement。我认为这应该在一个查询中完成,但我现在迷路了。

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)
                                          )
                             );

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)
                                          )
                             );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2024-09-02 23:13:55

每次迭代的 x 是元素序列(不包括第一个)。我怀疑你想要:

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

尽管如果你愿意,你也可以通过进一步简化将组密钥带出投影:

    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"
    //...

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"
    //...
走走停停 2024-09-02 23:13:55

我已经成功地编译了它,并且似乎可以工作,但可能需要一些性能调整,或者我正在做一些效率低下的事情。你怎么认为?

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)
                                          ));

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)
                                          ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文