I'm using LINQ to SQL to query my database, I have a query very similar to this:
var result = from db.MyTable.Where(d => (double)d.Price >= minValue)
I need the where clause to have a d.Proce >= minValue
, and d.Price =< maxValue
(like a T-SQL BETWEEN
clause).
How can I do this?
How about this:
var result = from db.MyTable.Where(d => (double)d.Price >= minValue
&& (double)d.Price <= maxValue)
Just for completeness; if you are building a query based on different inputs you can compose it with successive Where
calls:
IQueryable<SomeType> query = db.MyTable;
if(minValue != null) // a Nullable<double>
{
var actualMin = minValue.Value;
query = query.Where(d => (double) d.Price >= actualMin);
}
if(maxValue != null) // a Nullable<double>
{
var actualMax = maxValue.Value;
query = query.Where(d => (double) d.Price <= actualMax);
}
// keep working with "query", for example, query.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With