2

I have the following method inside my asp.net mvc web application :-

public SystemInformation GetSystemInfo(int pagesize)           
{
    var d = DateTime.Today;
    SystemInformation s = new SystemInformation()
    {                            
       DeleteNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd.Date) == EntityFunctions.TruncateTime(d) && a.AuditAction.Name.ToLower() == "delete")).Count(),

    };
    return s;
}

But the above is raising the following exception:-

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

4

It is a.DateTimeEnd.Date that cannot be translated. The equivalent is EntityFunctions.TruncateTime(a.DateTimeEnd) . You do not need/cannot use both.

DateTime.Date :

A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00)

EntityFunctions.TruncateTime :

The input date with the time portion cleared.

Colin
  • 22,328
  • 17
  • 103
  • 197
2

LINQ to Entities cannot translate most .NET Date methods (including the casting you used) into SQL since there is no equivalent SQL.

In your case LINQ cannot translate this

(EntityFunctions.TruncateTime(a.DateTimeEnd.Date)

The solution is to cast the object outside the LINQ statement and then pass in a value.

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • but how i can do this in my case , as i am using the date inside a where,, so i can not cast the object ouside my where ()? can u advice ? – John John Dec 05 '13 at 11:23