I started an asp.net mvc
project and I wanna filter requesting data from database with dynamic linq/lambda
. Some part of my code is as below:
IQueryable<VehicleMilage> query = null;
if (Fr_DeviceId == null && Fr_ContractId == null && From_Date == null && To_Date == null && From_QueryDate == null && To_QueryDate == null)
{
query = (db.VehicleMilage).OrderBy(x => x.Id).Skip(skip).Take(rows);
}
else
{
query = (db.VehicleMilage);
if (Fr_DeviceId != null)
{
int a = int.Parse(Fr_DeviceId);
query = query.Where(x => x.Fr_DeviceId == a);
}
if (Fr_ContractId != null)
{
int b = int.Parse(Fr_ContractId);
query = query.Where(x => x.Fr_ContractId == b);
}
if (From_Date != null)
{
query = query.Where(x => x.From_Date.Date >= FromDate_ConvertedToDateTime1.Date);
}
if (To_Date != null)
{
query = query.Where(x => x.To_Date.Date <= ToDate_ConvertedToDateTime1.Date);
}
if (From_QueryDate != null)
{
query = query.Where(x => x.CreateDate.Date >= FromQueryDate_ConvertedToDateTime1.Date);
}
if (To_QueryDate != null)
{
query = query.Where(x => x.CreateDate.Date <= ToQueryDate_ConvertedToDateTime1.Date);
}
query = query.OrderBy(x => x.Id).Skip(skip).Take(rows);
}
My data in database is at least 2000000 records and I must define the variable query
as IQueryable
.
In block of else
I must filter the query
with every parameter that is not null but I encounter the error
The specified type member 'Date' is not supported in LINQ to Entities.
If I define the variable of query
as List
, then I could put ToList()
before Where
In every if
in block of else
that is related to datetime as below but in this project because of heavy data I cannot do this:
query = query.ToList().Where(x => x.From_Date.Date >= FromDate_ConvertedToDateTime1.Date);