Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Sum of product of 2 columns in LINQ

Tags:

c#

linq

Find sum of product of 2 columns in LINQ

I have a list of educationprogram objects with 2 properties NOOfPerson & Hours.

I want a sum of product of 2 properties

SUM[NOOfPersons*Hours] 

How can I write a LINQ query for this?

List<educationprogram> edu = (from e in dbContext.educationprograms
                              where e.YearId == 2015
                              select e).ToList();

This returns the list of object I have to use. But how can I return SUM[Col1*Col2]?

like image 921
Sebastian Avatar asked Oct 22 '25 04:10

Sebastian


1 Answers

If the two columns are under the educationprograms table then:

var sum = dbContext.educationprograms.Sum(ep => ep.NoOfPeople * ep.Hours);

you can also add a Where clause:

var sum2 = dbContext.educationprograms.Where(e => e.Year == 2015).Sum(ep => ep.NoOfPeople * ep.Hours);
like image 126
Amir Popovich Avatar answered Oct 23 '25 18:10

Amir Popovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!