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?
May be something like this
list.GroupBy(item => item.Id)
.Where(group => group.Count() > 1)
.Select(group => new {
Id = group.Key,
Count = group.Count() });
Stolen from here :
var distinctItems =
from list in itemsList
group list by list.ItemCode into grouped
where grouped.Count() > 1
select grouped;
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());
}
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.