What is the SQL WHERE equivalence in lambda expressions?

Here is what I have:

decimal sum = _myDB.Products.Sum(p => p.Price).GetValueOrDefault();

I also have two dates: DateTime start, DateTime end
I want to get the sum of all product prices between the beginning and the end, but I can not figure out how to turn on the variables in the equation lambda.

How do you include variables in the lambda equation to give it some specifications?

+3
source share
3 answers

Use Enumerate. Where

decimal sum = _myDB.Products
                   .Where(p => (p.Date >= start) && (p.Date <= end) )
                   .Sum(p => p.Price)
                   .GetValueOrDefault();
+11
source
 decimal sum = _myDB.Products
.Where(p => p.Start >= mystartDate && p.End <= myenddate)
.Sum(p => p.Price)

Sorry my syntax. But hopefully you have an idea.

EDIT: Fixed after Reid's suggestion.
Old code (incorrect)

 decimal sum = _myDB.Products
.Sum(p => p.Price)
.Where(p => p.Start >= mystartDate && p.End <= myenddate)
+1
source
_myDB.Products
.Where(p => p.start >= "somevalue")
.Where(p => p.end <= "somevalue")
.Sum(p => p.Price).GetValueOrDefault();
0

Source: https://habr.com/ru/post/1711113/


All Articles