How to create linq to get all records group by columnname

63 Views Asked by At

I have written below code but I am getting only one record of each ExchangeDataId. Please help to get all the data which is groupby ExchangeDataId.

 var Report = ExchangeLeads.GroupBy(c => new
            {
                c.ExchangeDataId
            }).SelectMany(grouping=>grouping.Take(1)).ToList();
2

There are 2 best solutions below

0
On BEST ANSWER
ExchangeLeads
  .GroupBy(c => c.ExchangeDataId)
  .Select(group=> new { Id = group.Key, Items = group.ToList() })
  .ToList()
0
On

You can try the following,

var result = ExchangeLeads.ToLookup(x => x.ExchangeDataId).Select(x => new { Id = x.Key, ExchangeLeads = x.ToList() }).ToList();

Lookup is more efficient than GroupBy here.