To make my top layers more readable I usually make extension methods to encapsulate long hard-to-read queries into something as simple as db.Matches.By(period)
This 'By' method looks something like this:
public static IQueryable<PlayedMatch> By(this IQueryable<PlayedMatch> matches, Period period)
{
return matches.Where(pm => pm.Details.DateTime >= period.Start && pm.Details.DateTime < period.End);
}
Problem is that I would like to have something similar for querying Navigation Properties, so I could do something like this:
var query = Db.Players.Select( p => new
{
Player = p,
TotalPoints = p.Matches.By(period).Sum(m => m.Points)
});
Problem is that first of all Navigation Properties are of type ICollection<>
. Second is that when I change the extension method to use IEnumerable<> or ICollection<> I get the following exception while running the query:
LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'1[Match] By(System.Collections.Generic.ICollection`1[Match], Period)' method, and this method cannot be translated into a store expression.
Question:
Is there any other way for me to encapsulate queries on navigation properties like I do with my normal queries?