Sam
Sam

Reputation: 1908

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.Infrastructure.DbQuery'

I receive this error when I put the following code:

var rec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders);
...
...
...
//error on next line: Cannot implicitly convert type 'System.Linq.IQueryable<LocalDB.tblOrder>' to 'System.Data.Entity.Infrastructure.DbQuery<LocalDB.tblOrder>'. An explicit conversion exists (are you missing a cast?)
rec = rec.Where(x => (x.WarehouseId == iWarehouseId) && (x.OrderId == iOrderId));

Any idea how to fix it?

Thanks :)

Upvotes: 1

Views: 1045

Answers (1)

Jean Hominal
Jean Hominal

Reputation: 16796

The rec variable is typed implicitly as a DbQuery<tblOrder> object (because tblOrders is a DbQuery<tblOrder> object, and DbQuery<T>.AsNoTracking returns an object of the same type).

So, your var declaration looks like that to the compiler:

DbQuery<tblOrder> rec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders);

However, Where is only defined to return an IQueryable<T> object, which does not necessarily fit as a DbQuery<tblOrder> object.

It should be possible to avoid these issues with an explicit typing of the rec variable:

IQueryable<tblOrder> rec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders);

Upvotes: 2

Related Questions