I want to be able to write the following code for a LINQ to Entities query (EF6)
Repo.ContactRelations.WhereActive()
.Select(r => new ContactModel
{
Id = r.Contact.Id,
FirstName = r.Contact.FirstName,
LastName = r.Contact.Surname,
WorkEmail = r.Contact.WorkEmail
})
Without the WhereActive() method, I would have to repeat the following expression in numerous places:
Repo.ContactRelations.Where(c => c.EndDate == null || c.EndDate > DateTime.Today)
I tried to write a simple extension method, but it gave an error "LINQ to Entities does not recognize the method WhereActive"
public static IEnumerable<T> WhereActive<T>(this IEnumerable<T> source) where T : class, IMayExpire
{
return source.Where(c => c.EndDate == null || c.EndDate > DateTime.Today);
}
After some reading on LINQ to Entities vs LINQ to Object, and Expression trees vs Func<>, I realized I would need to build a full Expression tree to express my intention.
I'm not sure how to do it, may I get some help please?