When using Entity Framework, it seems that it is not properly generating SQL if I use interfaces (or generics) in lambda functions.
Interface:
interface ILogEntry
{
DateTime StartTime { get; set; }
}
Entity that implements this interface:
class ReceiverLimitCache : ILogEntry
{
pbulic DateTime StartTime { get; set; }
...
}
Utility class for handling these logs:
public class DBLog<T> : ILog<T>
where T : ILogEntry
{
private IEnumerable<T> _source;
public DBLog(IEnumerable<T> source)
{
_source = source;
}
public virtual T GetAtTime(DateTime time)
{
return _source
.Where(t => t.StartTime <= time)
.OrderByDescending(t => t.StartTime)
.FirstOrDefault();
}
}
The log is instantiated with the relevant collection from the DB Context.
However, when I call this function GetAtTime(...), EF is executing the following SQL:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[StartTime] AS [StartTime],
[Extent1].[ReceiverID] AS [ReceiverID],
[Extent1].[Limit] AS [Limit]
FROM [Results].[ReceiverLimitCache] AS [Extent1]
Which does not seem to include the time condition, ignores the index on the table and slows my program down to a crawl. (I can only assume EF is doing the sorting and selecting and everything else locally).