使用双重分组后如何平展 Linq 查询
本文关键字:何平 Linq 查询 | 更新日期: 2023-09-27 18:32:26
>假设我有一个表示订单行的类,例如
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>
我试过了 查询 1.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
);
此代码:
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);
与以下内容相同:
var q2 = from r1 in query1
from r2 in r1.Results
from r3 in r2
select r3;
var output = q2.ToList();
试试这个:
var flattenedLines = from outerGroup in query1
from innerGroup in outerGroup.Results
from line in innerGroup
select line;
或
var flattenedLines = query1
.SelectMany(outerGroup => outerGroup.Results, (outerGroup, innerGroup) => innerGroup)
.SelectMany(x => x);