【问题标题】:LINQ query with distinct count具有不同计数的 LINQ 查询
【发布时间】:2014-02-11 15:11:29
【问题描述】:

我正在尝试在 C# 中构建一个 LINQ 查询,它将为我提供一个数据集中列中不同值的列表,其中每行都有一个计数。结果应该是这样的。

State   Count
AL       55
AK       40
AZ       2

这是执行此操作的 SQL。

SELECT name, COUNT(*) AS count
  FROM architecture arch
  GROUP BY name
  ORDER BY name

我已经找到了 LINQ 来获取 DISTINCT 值。

var query = ds.Tables[0].AsEnumerable()
    .OrderBy(dr1 => dr1.Field<string>("state"))
    .Select(dr1 => new {state = dr1.Field<string>("state")})
    .Distinct().ToList();

但我不知道如何让每个不同值的 COUNT(*) 在 LINQ 中工作。知道如何将其添加到 LINQ 查询中吗?

【问题讨论】:

    标签: c# sql linq


    【解决方案1】:

    您需要根据 State 和从组中选择的计数对结果进行分组,例如:

    var query = ds.Tables[0].AsEnumerable()
                .GroupBy(r => r.Field<string>("state"))
                .Select(grp => new 
                                { 
                                    state = grp.Key, 
                                    Count = grp.Count()
                                })
                .OrderBy(o => o.state)
                .ToList();
    

    【讨论】:

    • 缺少订购,但打败了我 :)
    【解决方案2】:

    state 列的值对所有行进行分组。然后通过分组键对组进行排序。最后一步 - 将每个组投影到具有分组键(状态)和组中行数的匿名对象中:

    var query = ds.Tables[0].AsEnumerable()
                  .GroupBy(r => r.Field<string>("state"))
                  .OrderBy(g => g.Key)
                  .Select(g => new { State = g.Key, Count = g.Count() })
                  .ToList();
    

    查询语法看起来像(我将跳过转换为列表,以避免混合语法):

    var query = from r in ds.Tables[0].AsEnumerable()
                group r by r.Field<string>("state") into g
                orderby g.Key
                select new {
                    State = g.Key,
                    Count = g.Count()
                };
    

    【讨论】:

      【解决方案3】:

      我认为你需要 GroupBy

      var query = ds.Tables[0].AsEnumerable()
                    .GroupBy(dr1 => dr1.Field<string>("state"))
                    .Select(g => new {state = g.Key, count = g.Count())
                    .ToList();
      

      【讨论】:

        【解决方案4】:

        当您几乎可以逐字地将 SQL 查询转换为 LINQ 时,为什么还要打扰 Distinct?你可以这样做:

        var query = ds.Tables[0].AsEnumerable()
            .GroupBy(dr1 => dr1.Field<string>("state"))
            .Select(g => new {
                State = g.Key
            ,   Count = g.Count()
            })
            .OrderBy(p => p.State)
            .ToList();
        

        这会产生{State, Count} 对的列表。如果您更喜欢 state-to-count 字典,您可以像这样更改您的查询:

        var query = ds.Tables[0].AsEnumerable()
            .GroupBy(dr1 => dr1.Field<string>("state"))
            .ToDictionary(g => g.Key, g => g.Count());
        

        【讨论】:

        • 我认为你需要AsEnumerable() 来换取DataTable
        • @NewHire 你说得对,他的dr1.Field&lt;string&gt;("state") 可能与IQueryable&lt;T&gt; 不兼容。感谢您的评论!
        • 我认为这就是让我觉得我需要 Distinct 的原因。谢谢。
        • @John81 一般而言,Distinct() 是一种在您不需要分组的全部功能时避免使用更强大的GROUP BY 的方法。如果您需要不同的结果,但 Distinct() 还不够,那么 GroupBy() 很可能会完成这项工作。
        【解决方案5】:
        var query = ds.Tables[0].AsEnumerable()
                      .GroupBy(x=>x.Field<string>("state"))
                      .Select( g => new{
                           state = g.Key,
                           count = g.Count()
                      });
        

        【讨论】:

          【解决方案6】:

          猜猜看,group by 的等价物是group by :)

                  var query = from dr1 in ds.Tables[0].AsEnumerable()
                              group dr1 by dr1.Field<string>("state") into state
                              select new { State = state.Key, Count = state.Count() };
          

          【讨论】:

            【解决方案7】:
            var stat = from row in ds.Tables[0].AsEnumerable()
            group row by new
            {
              Col1 = row["Name"],
            } into TotalCount
            select new
            {
              ActionName = TotalCount.Key.Col1,
              ActionCount = TotalCount.Count(),
             };
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-06-20
              相关资源
              最近更新 更多