30% OFF - 10th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY10
Entity Framework The specified type member is not supported Exception
Exception: The specified type member '[MemberName]' is not supported in LINQ to Entities
Entity Framework cannot convert some data types to SQL such as DateTime.Date.
using (var context = new EntityContext()) { var fromDate = DateTime.Now.AddDays(-7).Date; var customer = context.Customers.Where(c => c.CustomerID == 1) .Include(c => c.Invoices) .Where(c => c.Invoices.Any(i => i.InvoiceDate.Date >= fromDate)) .FirstOrDefault(); }
StackOverflow Related Questions
- The specified type member 'Date' is not supported in LINQ
- The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties
Solution
The easiest solution to handle this exception is to use DbFunctions.TruncateTime method
using (var context = new CustomerContext()) { var fromDate = DateTime.Now.AddDays(-7).Date; var customer = context.Customers.Where(c => c.CustomerID == 1) .Where(c => c.Invoices.Any(i => DbFunctions.TruncateTime(i.InvoiceDate) >= fromDate)) .Select(c => new { c, Invoices = c.Invoices.Where(i => DbFunctions.TruncateTime(i.InvoiceDate) >= fromDate) }) .FirstOrDefault(); }
ZZZ Projects