1

我有以下获取 Name 和 TotalPoints 的查询,如下所示:

var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
                         .Where(row => row.RecievedPoints != "n/a")
                        .GroupBy(row => new { row.Name })
                        .Select(g => new
                        {
                            TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage))),
                            Name = g.Key.Name
                        })
                         select data).ToList();

我将有如下数据:

TotalPoints        Name
  5                  A
  10                 B
  5                  C
  15                 D
  5                  E

如果我们观察上面的列表,5 是最常见的。我必须从“gradeData”中获取该值。

我怎么能得到那个?

4

2 回答 2

6
var mostCommon = gradeData.GroupBy(x => x.TotalPoints)
    .OrderByDescending(g => g.Count())
    .Select(g => g.Key)
    .First();
于 2013-09-04T13:58:18.413 回答
2

下面的查询为您提供了最常见的点及其所有关联名称:

var result = gradeData.GroupBy(x => x.TotalPoints)
                      .OrderByDescending(x => x.Count())
                      .Select(g => new
                          {
                              TotalPoints = g.Key,
                              Names = g.Select(x => x.Name).ToList()
                          })
                      .First();
于 2013-09-04T13:58:40.380 回答