I am using Entity Framework Code First and ran into a small road block. I have a class "Person" defined as such:
public class Person
{
public Guid Id { get; set; }
public virtual ICollection<History> History { get; set; }
}
and a class "History" defined as such:
public class History
{
public Guid Id { get; set; }
public virtual Person Owner { get; set; }
public DateTime OnDate { get; set; }
}
However, when I call:
IEnumerable<History> results = person.History
.OrderBy(h => h.OnDate)
.Take(50)
.ToArray();
It appears to pull all of the history for the person, then order it and such in memory. Any recommendations on what I'm missing?
Thanks in advance!
Because you are querying an IEnumerable
(ie: LINQ to Objects) not IQueryable
(ie: LINQ to Entities) given by EF.
Instead you should use
IEnumerable<History> results = context.History.Where(h => h.Person.Id = "sfssd").OrderBy(h => h.OnDate).Take(50)