views:

1028

answers:

3

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?

+9  A: 

How about this:

var result = from db.MyTable.Where(d => (double)d.Price >= minValue 
                                         && (double)d.Price <= maxValue)
ck
+3  A: 

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();
Marc Gravell
A: 
            fromDate = System.Data.SqlTypes.SqlDateTime.MinValue.Value ;
            toDate =  System.Data.SqlTypes.SqlDateTime.MaxValue.Value ;
Akshat Sharma