【问题标题】:Group List<T> by Column and a count on multiple columns按列分组 List<T> 并计算多列
【发布时间】:2014-08-12 11:54:18
【问题描述】:

我试图在 List by 1 列上进行 GroupBy,然后对多列进行计数? 因此,下面的代码应该在这样的表格中显示结果:

Category       Parent        Child
Test1           1             1
Test2           1 
Test3           3             1 
Test4                         1 

我在下面尝试过这个,但在下面总是得到错误的结果。

var categories = GetCategories()
    .GroupBy(x => new{ x.Description })
    .Select(group => new{ Categories = group.Key, ParentCount = group.Count(),      
      ChildCount = group.Select (s => s.ChildCount).Count()});

Category       Parent        Child
Test1           1             1
Test2           1             1
Test3           3             3 
Test4           1             1  

public List<Category> GetCategories()
    {
        List<Category> CategoryList = new List<Category>();

            CategoryList.Add(new Category{
            ParentCount =101,
            ChildCount = 101,
            Description = "Test1"
            });

            CategoryList.Add(new Category{
            ParentCount =102,
            ChildCount = null,
            Description = "Test2"
            });

            CategoryList.Add(new Category{
            ParentCount =103,
            ChildCount = 103,
            Description = "Test3"
            });

            CategoryList.Add(new Category{
            ParentCount =103,
            ChildCount = null,
            Description = "Test3"
            });

            CategoryList.Add(new Category{
            ParentCount =103,
            ChildCount = null,
            Description = "Test3"
            });

            CategoryList.Add(new Category{
            ParentCount =null,
            ChildCount = 104,
            Description = "Test4"
            });

            return CategoryList;
    }

        public class Category
        {
            public string Description{get;set;}     
            public int? ParentCount{get;set;}
            public int? ChildCount{get;set;}    

        }

我知道这在 SQL 中很容易做到。

从 YOUR_TABLE 中选择描述、COUNT(ParentCount)、COUNT(ChildCount) 按描述分组

regards

【问题讨论】:

  • 给出你想要的结果和你得到的结果,可能还包括完成这项工作的 SQL
  • 谢谢,已按建议更新。

标签: c# linq linq-to-sql lambda


【解决方案1】:

你可以试试这个:

var categories = GetCategories().GroupBy(x => x.Description)
                                .Select(group => new { 
                                    Categories = group.Key, 
                                    ParentCount = group.Count(x => x.ParentCount!=null),      
                                    ChildCount = group.Count(x => x.ChildCount!=null) 
                                });

【讨论】:

  • 没错!有趣的是,我以为我早些时候尝试过。
【解决方案2】:

Select 替换为Where

var categories = GetCategories()
.GroupBy(x => new { x.Description })
.Select(group => new
{
  Categories = group.Key,
  ParentCount = group.Count(s=>s.ParentCount.HasValue),
  ChildCount = group.Where(s => s.ChildCount.HasValue).Count()
});

【讨论】:

  • 关闭,但是当我添加另一个条件时,它似乎失败了。 CategoryList.Add(new Category{ ParentCount =null, ChildCount = 104, Description = "Test4"
猜你喜欢
  • 1970-01-01
  • 2021-07-07
  • 2020-11-06
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多