I have this following query:
db.Users.AsQueryable()
.Where(u => u.Id = userResolver.LoggedUserId() && u.Packages.Where(p =>
p.StatusId == (int)PackageStatus.InProgress ||
p.StatusId == (int)PackageStatus.Delivered ||
p.StatusId == (int)PackageStatus.Shipped ||
p.StatusId == (int)PackageStatus.Waiting)
.Sum(p => p.Price) > u.MaxCredit)
.ToList()
What I'm trying to achieve is to group all the package status checks to an extension methods. Something like that:
db.Users.AsQueryable()
.Where(u => u.Id = userResolver.LoggedUserId() &&
u.Packages.Where(p => p.IsShippedOrInProgress())
.Sum(p => p.Price) > u.MaxCredit)
.ToList()
//This is the extension method
public static bool IsShippedOrInProgress(this Package p) {
return p.StatusId == (int)PackageStatus.InProgress ||
p.StatusId == (int)PackageStatus.Delivered ||
p.StatusId == (int)PackageStatus.Shipped ||
p.StatusId == (int)PackageStatus.Waiting)
}
When I view the sql query generated in the first example, everything seems ok, but when I'm using the second approach the part of the query that checks the status doesn't exists.