So like a good programer, Im doing TDD. Im using Nunit and Nsubustatue. On a MVC project that using EntityFramework code first.
So Im doing a search for purchase orders for using this code (other bits have been removed)
IQueryable<PurchaseOrderDTO> query = context.PurchaseOrder;
query = query.Where(x => x.CreateDate.Date >= purchaseOrderSearchParameters.CreatedDateFrom.Value.Date);
return query.ToList();
But then when I run it in MVC project, I get this error msg.
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
So when I change the code to use
System.Data.Entity.DbFunctions.TruncateTime,
it runs nice in EntityFramework
IQueryable<PurchaseOrderDTO> query = context.PurchaseOrder;
query = query.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.CreateDate) >= purchaseOrderSearchParameters.CreatedDateFrom);
return query.ToList();
But I get the nice new error msg when I run the unit tests
System.NotSupportedException : This function can only be invoked from LINQ to Entities.
So what is the best way to get both happy.