简体   繁体   中英

Group List<T> by Column and a count on multiple columns

I am trying to do GroupBy on a List by 1 column and then a count on multiple columns? Hence Code below should display the results in a table like this:

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

I've tried this below, but keep getting the wrong results below.

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;}    

        }

I know this is fairly easy to do in SQL.

SELECT Description, COUNT(ParentCount), COUNT(ChildCount) FROM YOUR_TABLE GROUP BY Description

regards

You could try this one:

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

Replace Select with 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()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM