使用 LINQ Group By 返回新的 XElement
我有以下代码,但让自己感到困惑:
我有一个查询返回一组已被标识为重复项的记录,然后我想为每个记录创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次迭代的 x 是元素序列(不包括第一个)。我怀疑你想要:
尽管如果你愿意,你也可以通过进一步简化将组密钥带出投影:
x, per iteration, is the sequence of elements (excluding the first one). I suspect you want:
Although you could also bring the group-key out in the projection if you wanted, by simplifying further:
我已经成功地编译了它,并且似乎可以工作,但可能需要一些性能调整,或者我正在做一些效率低下的事情。你怎么认为?
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?