1

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.

Auguste
  • 2,007
  • 2
  • 17
  • 25
Ashley Kilgour
  • 1,110
  • 2
  • 15
  • 33
  • Possible duplicate of [EntityFunctions.TruncateTime and unit tests](http://stackoverflow.com/questions/9585203/entityfunctions-truncatetime-and-unit-tests) – Charles Mager May 24 '16 at 15:50
  • Here is the solution: http://stackoverflow.com/questions/11597373/the-specified-type-member-date-is-not-supported-in-linq-to-entities-exception – Dexion May 24 '16 at 16:02

0 Answers0