[...]
_errors.Where(e => e.Created.Date>= start.Date&& e.Created.Date<=
end.Date).GroupBy(e => e.Created.Date).ToDictionary(g => g.Key.Date, g
=> g.Count());
The specified type member 'Date' is not supported in LINQ to Entities.
Only initializers, entity members, and entity navigation properties
are supported.
Any idea what might be wrong?
Sure. The error message is quite clear: you are using LINQ to Entities,
and like LINQ to SQL, there are restrictions on what you can put in a
lambda expression.
With LINQ to Objects, the code just winds up executing as straight .NET
code. You have full access to all type members in that case.
But with other forms of LINQ, including LINQ to Entities, the lambda has
to be convertible to some domain-specific instructions. If a property
like DateTime.Date is not available in that domain, the code fails to run..
Work-arounds include restating the lambda in terms of an entity member
that does exist (for example, it may be that you can effective recreate
the Date property using some Year, Month, and Day properties that might
be available), or use LINQ to Objects instead of going directly to the
Entities framework.
Pete
Hello,
I did try other options. For example:
public IDictionary<DateTime, Int32> CountByCreated(DateTime start,
DateTime end) {
return _errors
.Where(e => e.Created.Date >= start && e.Created.Date <= end)
.GroupBy(e => EntityFunctions.TruncateTime(e.Created))
.ToDictionary(g => g.Key.Value, g => g.Count());
}
Where I get the error:
The specified type member 'Date' is not supported in LINQ to Entities.
Only initializers, entity members, and entity navigation properties
are supported.
Or:
public IDictionary<DateTime, Int32> CountByCreated(DateTime start,
DateTime end) {
DateTime startDate = start.Date;
DateTime endDate = end.Date;
return _errors.Where(e => e.Created.Date >= startDate &&
e.Created.Date <= endDate).GroupBy(e => new {e.Created.Year,
e.Created.Month, e.Created.Day}).ToDictionary(g => g.Key, g =>
g.Count());
}
Where I get the error:
Cannot implicitly convert type
'System.Collections.Generic.Dictionary<AnonymousType#1,int>' to
'System.Collections.Generic.IDictionary<System.DateTime,int>'. An
explicit conversion exists (are you missing a cast?)
With this last one I tried a few more options with no luck.
But I think the way to go should be using
EntityFunctions.TruncateTime, right?
But I keep getting errors.
Thank You,
Miguel