【问题标题】:Take result with max count using LINQ使用 LINQ 获取最大计数的结果
【发布时间】:2013-10-30 13:04:42
【问题描述】:

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

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

任何有关如何实现此功能的帮助将不胜感激。

问候,

凯尔

【问题讨论】:

    标签: c# .net linq datatable max


    【解决方案1】:

    GroupBy 应该让你这样做,例如

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

    【讨论】:

    • 我非常喜欢这个答案;它工作得很好,而且非常甜美、简单而且切中要害。完美!
    【解决方案2】:
    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 
                                      }
                                  });
    

    【讨论】:

    • Tape=x.Key 应该是 Tape=g.Key,x 在那个上下文中不存在,
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:
          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
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-12
          • 1970-01-01
          • 1970-01-01
          • 2012-04-23
          • 2014-03-25
          • 2014-09-02
          • 1970-01-01
          • 2019-06-22
          相关资源
          最近更新 更多