gpt4 book ai didi

c# - 使用双分组后如何展平 Linq 查询?

转载 作者:太空狗 更新时间:2023-10-29 20:47:40 28 4
gpt4 key购买 nike

假设我有一个代表订单行的类,例如

public class Line
{
public string Code ;
public string No ; // Invoice Number
public DateTime Date ;
public string Product ;
public decimal Quantity ;
}

和行列表,例如

List<Line> myList = new List<Line>();
myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) , Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) , Product = "Y", Quantity= 1m});

myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) , Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) , Product = "Y", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1003" ,Date = new DateTime(2012,4,3) , Product = "Z", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1004" ,Date = new DateTime(2012,4,4) , Product = "X", Quantity= 1m});

myList.Add(new Line() { Code = "ABC003", No = "1005" ,Date = new DateTime(2012,4,4) , Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) , Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) , Product = "Y", Quantity= 1m});

我希望检索客户代码有多个发票的所有行。为此,我首先按代码、编号和日期分组,然后按客户代码分组,对于有两条或更多记录的任何客户,我选择除第一条记录以外的所有记录。

像这样:

var query1 = 
(from r in myList
group r by new { r.Code , r.No , r.Date } into results
group results by new { results.Key.Code } into results2
where results2.Count() > 1
select new
{
results2.Key.Code ,
Count = results2.Count(),
Results = results2.OrderBy(i=>i.Key.Date).Skip(1).ToList()
// Skip the first invoice
}
).ToList();

query1 现在包含正确的记录,但包含在 IGrouping 中,我无法将结果作为 List<Line> 输出

我试过了 query1.SelectMany(r=>r.Results).ToList();

但这仍然给我留下了 IGrouping,这就是我被困的地方。

我可以求助于嵌套的 for 循环

List<Line> output = new List<Line>();
foreach (var r1 in query1)
{
foreach(var r2 in r1.Results)
foreach(var r3 in r2)
output.Add(r3);
}

但是有更好的/Linq 方式吗?

实际输出应该是四行

// Code    No    Date              Product Quantity 
// ABC002 1003 03/04/2012 00:00:00 Z 1
// ABC002 1004 04/04/2012 00:00:00 X 1
// ABC003 1006 04/04/2012 00:00:00 X 1
// ABC003 1006 04/04/2012 00:00:00 Y 1

最佳答案

你会踢自己:

query1.SelectMany(q => q);

ABC002 1003 3/04/2012 12:00:00 AM Z 1
ABC002 1004 4/04/2012 12:00:00 AM X 1
ABC003 1006 4/04/2012 12:00:00 AM X 1
ABC003 1006 4/04/2012 12:00:00 AM Y 1

query1 的返回值是 IGrouping 的可枚举值(我删除了你的列表),而 IGrouping 本身也是可枚举值,因此我们可以直接压平。

参见此处:http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/09/28/9836.aspx

编辑:记得我还简化了您的代码:

var query1 = 
(from r in myList
group r by new { r.Code , r.No , r.Date } into results
group results by new { results.Key.Code } into results2
where results2.Count() > 1
from result in results2.OrderBy(i=>i.Key.Date).Skip(1)
select result
);

关于c# - 使用双分组后如何展平 Linq 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com