Aggregate function Count() using dynamic linq query

c# dynamic-linq linq linq-to-entities sql

Question

I am trying to use Aggregate function Count() on some column using dynamic linq query but I am not able to achieve, what I am exactly looking is

Select Count(Id),Id 
from Table1 
Group By Id 
Having Count(Id) > 1

I want to convert the same query to dynamic linq query, any suggestions on how to achieve this?

1
2
9/25/2013 11:44:22 AM

Popular Answer

The following two queries will give you the exact same result: first with lambda second result without.

        var Table1 = new List<Row>();

        for (int i = 0; i < 10; i++)
        {
            for (int m = 0; m < 5; m++)
            {
                for (int x = 0; x < i + m; x++)
                {
                    Table1.Add(new Row() { Id = m });
                }
            }
        }

        var result = Table1.GroupBy(row => row.Id)
                    .Where(p => p.Count() > 1);

        var resultB = from row in Table1
                      group row by row.Id into rowGrouped
                      where rowGrouped.Count() > 1
                      select rowGrouped;

        foreach (var r in resultB)
        {
            Console.WriteLine("ID {0} count: {1}", r.Key, r.Count());
        }
0
9/25/2013 12:09:46 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow