Kojof
Kojof

Reputation: 439

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

Upvotes: 0

Views: 619

Answers (2)

S.L.
S.L.

Reputation: 1076

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

Upvotes: 1

Christos
Christos

Reputation: 53958

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

Upvotes: 1

Related Questions