使用LINQ获取最大计数结果

时间:2013-10-30 13:04:43

标签: c# .net linq datatable max

我在内存中有一个数据表,如下所示:

Tape               Mask                Count
BT1                DML69               3452
BT2                DML69               1569
BT2                DML87               2745
BT3                DML69               3215
BT3                DML87               1542
BT4                DML87               3214
BT5                DML69               2132
BT5                DML87               1241

我需要一个LINQ查询,它将返回每个磁带的数据行,每个磁带一个结果,包括只有最大计数的掩码。即

Tape               Mask                Count
BT1                DML69               3452
BT2                DML87               2745
BT3                DML69               3215
BT4                DML87               3214
BT5                DML69               2132

我尝试过几种不同的方法,但都没有成功。这是我最近的尝试:

        foreach (string tape in singOne.GetDistinctTapes(
                                    converter.ConvertProcessesIDToSingOneID(selectedWafer)))
        {
            var tapeMaskQuery =
                from row in tempTable.AsEnumerable()
                where row.Field<string>("location1") == tape
                select row;

            if (tapeMaskQuery.Count() == 1)
            {
                tapeMaskCountTable.Rows.Add(tapeMaskQuery.ElementAt(0));
            }
            else
            {
                var maxMaskQuery =
                    (from fields in tapeMaskQuery.AsEnumerable()
                    select new
                    {
                        tape = fields.Field<string>("location1"),
                        mask = fields.Field<string>("mask"),
                        count = fields.Field<Int64>("count(*)")
                    }).Max(x => x.count);
                tapeMaskCountTable.Rows.Add(maxMaskQuery);
            }
        }

这里,singOne.GetDistinctTapes()只返回晶圆上不同磁带的列表。 tempTable是一个DataTable,它以本文第一个表的形式出现。我检查每个磁带是否有多个结果;如果有,那么我运行“maxMaskQuery”,它似乎只返回一个DataRow,只包括count列,而不是tape和mask列。我需要最后三列才能出现,就像在这篇文章的第二个表中一样。

非常感谢任何有关如何实现这一点的帮助。

此致

凯尔

5 个答案:

答案 0 :(得分:7)

GroupBy应该允许你这样做。

var query = tapesTable.GroupBy(x => x.Tape)
                      .Select(x => x.OrderByDescending(t => t.Count)
                                    .First());

答案 1 :(得分:1)

var result = data.GroupBy(x => x.Tape)
                 .Select(g => {
                               var maxItem = g.OrderByDescending(x => x.Count)
                                              .FirstOrDefault();
                               return new { 
                                    Tape = x.Key, 
                                    maxItem.Mask, 
                                    maxItem.Count 
                                  }
                              });

答案 2 :(得分:0)

只需回答第一句话,请使用GroupBy + OrderByDescendingFirst

IEnumerable<DataRow> tapeMaskQuery = table.AsEnumerable()
    .GroupBy(row => row.Field<string>("Tape"))
    .Select(group => group.OrderByDescending(r => r.Field<int>("Count"))
                          .First()); 

答案 3 :(得分:0)

如果你需要保持掩码的最大数量,你可以这样写:

var maxMaskQuery =
                (from fields in tapeMaskQuery.AsEnumerable()
                select new
                {
                    tape = fields.Field<string>("location1"),
                    mask = fields.Field<string>("mask"),
                    count = fields.Field<Int64>("count(*)")
                }).OrderByDescending(x => x.count).FirstOrDefault();

答案 4 :(得分:-1)

  var AllSalesForThisMonth = db.Salestable.ToList(); // list the data from the table
    
    var Groupthesale = AllSalesForThisMonth.GroupBy(x => x.EmployeeId).ToList();// group them with something unique
                    
 if(Groupthesale!=null)
 {
    var thelargestcountfromthegroup = Groupthesale.OrderBy(c=>c.Count()).Last();// final 
     result
 }