0

我在内存中有一个数据表,看起来像这样:

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 查询,它将为每个磁带返回一个数据行,每个磁带一个结果,仅包括具有最大计数的掩码。IE

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,其中只包括计数列,而不是磁带和掩码列。最后,我需要所有三列都出现,就像在这篇文章的第二个表中一样。

任何有关如何实现这一点的帮助将不胜感激。

问候,

凯尔

4

5 回答 5

9

GroupBy应该让你这样做,例如

var query = tapesTable.GroupBy(x => x.Tape)
                      .Select(x => x.OrderByDescending(t => t.Count)
                                    .First());
于 2013-10-30T13:09:29.510 回答
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 
                                  }
                              });
于 2013-10-30T13:11:40.380 回答
0

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

IEnumerable<DataRow> tapeMaskQuery = table.AsEnumerable()
    .GroupBy(row => row.Field<string>("Tape"))
    .Select(group => group.OrderByDescending(r => r.Field<int>("Count"))
                          .First()); 
于 2013-10-30T13:11:50.067 回答
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();
于 2013-10-30T13:11:58.110 回答
-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
 }
于 2021-05-17T07:22:42.287 回答