-1

I want to group by a datatable by the columns which are present in a List. Moreover I want to sum a column using group by result.

How to create a dynamic linq query for this?

Shivani
  • 1
  • 1

2 Answers2

0

In case you want to use a dynamic linq query for this, you can use System.Linq.Dynamic.Core.

The code could look like:

var result = context.Posts.GroupBy("BlogId").Select("new(Key, Sum(NumberOfReads) AS TotalReads)");

See also

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
-1

Just group by the identifier you need and then sum the column as below.

    var lstYourClass = lstYourClass .GroupBy(x => x.Id).Select(z => new YourClassType
    {
    
    Amount= z.Sum(a => a.Amount),
    
    
    }).ToList();

Hope it helps :)

Kristo
  • 1,339
  • 12
  • 22