使用双重分组后,如何展平Linq查询?

时间:2012-04-26 20:13:19

标签: c# linq

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

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

我希望检索客户代码有多个发票的所有行。 要做到这一点,我首先按照Code,No和Date进行分组,然后按客户代码对其进行分组,对于任何两个或更多记录的客户,我选择除了第一条记录以外的所有记录。

像这样:

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 =&GT; 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 

3 个答案:

答案 0 :(得分:10)

你要踢自己:

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

答案 1 :(得分:9)

此代码:

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

答案 2 :(得分:3)

试试这个:

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