I have complex query:
var containers = this.Repository.Containers
.Include(x => x.PostsContainers)
.ThenInclude(x => x.Post)
.ThenInclude(x => x.TasksPosts)
.ThenInclude(x => x.Task)
.ThenInclude(x => x.AssignedToUser);
var items = containers.Where(x => x.PostsContainers
.Any(y => y.Post.TasksPosts
.Any(z => z.Task.DateDue <= DateTime.UtcNow.AddDays(7)
&& !z.Task.Completed
&& z.Task.AssignedToUserId.Value == userId)));
I got items but I also need to sort these items by Task.DueDate and extract AssignedToUser name.
What's the best way to do it (with good performance, without code duplication)? Maybe I need to rewrite my code?
Try to refactor it this way:
var dateDue = DateTime.UtcNow.AddDays(7)
var result = (from c in this.Repository.Containers
from pc in c.PostsContainers
from tp in pc.Post.TasksPosts
where tp.Task.DateDue <= dateDue
&& !tp.Task.Completed
&& tp.Task.AssignedToUserId == userId
orderby tp.Task.DueDate
select new
{
Container = c,
tp.Task.AssignedToUser
})
.ToList();
Related
I have three classes:
OrderSet - Order (1:n) and OrderDetail (1:n)
OrderSet and Order has each a property 'Status'.
I want a receive a construct with all OrderSet with Status='open' and all regarding Orders with Status='open'.
I tried this:
var orderSet = db.OrderSet
.Where(x => x.Status == 'Open')
.Where(x => x.Order.Any(y => y.Status == 'Open'))
.Include(x => x.Order.Select(q => q.OrderDetail))
But I got all Orders, also with Status 'Closed'.
What is my fault?
Thank you in advance.
If you want to ignore OrderSets which contain any Order that doesn't have an "Open" status, you may use:
var orderSets = db.OrderSet
.Where(os => os.Status == "Open" && os.Order.All(o => o.Status == "Open")
.Include(os => os.Order.Select(o => o.OrderDetail));
If you want to include those OrderSets but only ignore the child Orders that don't meet the said condition, there's probably no way to do that without modifying the collection of OrderSets returned by the query.
If that's what you want to do, one way to achieve that would be like this:
var orderSets = db.OrderSet
.Where(os => os.Status == "Open")
.Include(os => os.Order.Select(o => o.OrderDetail)).ToList();
foreach (var orderSet in orderSets)
{
orderSet.Order.RemoveAll(o => o.Status == "Open");
}
I have the following Entity Framework 2.0 query:
var user = context.Users.AsNoTracking()
.Include(x => x.UserSkills).ThenInclude(x => x.Skill)
.Include(x => x.UserSkills).ThenInclude(x => x.SkillLevel)
.FirstOrDefault(x => x.Id == userId);
var userSkills = user.UserSkills.Select(z => new {
SkillId = z.SkillId,
SkillLevelId = z.SkillLevelId
}).ToList()
Then I tried the following query:
var lessons = _context.Lessons.AsNoTracking()
.Where(x => x.LessonSkills.All(y =>
userSkills.Any(z => y.SkillId == z.SkillId && y.SkillLevelId <= z.SkillLevelId)))
.ToList();
This query evaluates locally and I get the message:
The LINQ expression 'where (([y].SkillId == [z].SkillId) AndAlso ([y].SkillLevelId <= [z].SkillLevelId))' could not be translated and will be evaluated locally.'.
I tried to solve it using userSkills instead of user.UserSkills but no luck.
Is there a way to run this query on the server?
You should try limiting the usage of in-memory collections inside LINQ to Entities queries to basically Contains on primitive value collection, which currently is the only server translatable construct.
Since Contains is not applicable here, you should not use the memory collection, but the corresponding server side subquery:
var userSkills = context.UserSkills
.Where(x => x.UserId == userId);
var lessons = context.Lessons.AsNoTracking()
.Where(x => x.LessonSkills.All(y =>
userSkills.Any(z => y.SkillId == z.SkillId && y.SkillLevelId <= z.SkillLevelId)))
.ToList();
or even embed the first subquery into the main query:
var lessons = context.Lessons.AsNoTracking()
.Where(x => x.LessonSkills.All(y =>
context.UserSkills.Any(z => z.UserId == userId && y.SkillId == z.SkillId && y.SkillLevelId <= z.SkillLevelId)))
.ToList();
Use Contains on the server then filter further on the client:
var userSkillIds = userSkills.Select(s => s.SkillId).ToList();
var lessons = _context.Lessons.AsNoTracking()
.Where(lsn => lsn.LessonSkills.All(lsnskill => userSkillIds.Contains(lsnskill.SkillId)))
.AsEnumerable() // depending on EF Core translation, may not be needed
.Where(lsn => lsn.LessonSkills.All(lsnskill => userSkills.Any(uskill => uskill.SkillId == lsnskill.SkillId && lsnskill.SkillLevelId <= uskill.SkillLevelId)))
.ToList();
I have a massive LINQ query that fetches information that looks like this:
In other words, first-level categories, which own second-level categories, which own third level categories. For each category we retrieve the number of listings it contains.
Here is the query:
categories = categoryRepository
.Categories
.Where(x => x.ParentID == null)
.Select(x => new CategoryBrowseIndexViewModel
{
CategoryID = x.CategoryID,
FriendlyName = x.FriendlyName,
RoutingName = x.RoutingName,
ListingCount = listingRepository
.Listings
.Where(y => y.SelectedCategoryOneID == x.CategoryID
&& y.Lister.Status != Subscription.StatusEnum.Cancelled.ToString())
.Count(),
BrowseCategoriesLevelTwoViewModels = categoryRepository
.Categories
.Where(a => a.ParentID == x.CategoryID)
.Select(a => new BrowseCategoriesLevelTwoViewModel
{
CategoryID = a.CategoryID,
FriendlyName = a.FriendlyName,
RoutingName = a.RoutingName,
ParentRoutingName = x.RoutingName,
ListingCount = listingRepository
.Listings
.Where(n => n.SelectedCategoryTwoID == a.CategoryID
&& n.Lister.Status != Subscription.StatusEnum.Cancelled.ToString())
.Count(),
BrowseCategoriesLevelThreeViewModels = categoryRepository
.Categories
.Where(b => b.ParentID == a.CategoryID)
.Select(b => new BrowseCategoriesLevelThreeViewModel
{
CategoryID = b.CategoryID,
FriendlyName = b.FriendlyName,
RoutingName = b.RoutingName,
ParentRoutingName = a.RoutingName,
ParentParentID = x.CategoryID,
ParentParentRoutingName = x.RoutingName,
ListingCount = listingRepository
.Listings
.Where(n => n.SelectedCategoryThreeID == b.CategoryID
&& n.Lister.Status != Subscription.StatusEnum.Cancelled.ToString())
.Count()
})
.Distinct()
.OrderBy(b => b.FriendlyName)
.ToList()
})
.Distinct()
.OrderBy(a => a.FriendlyName)
.ToList()
})
.Distinct()
.OrderBy(x => x.FriendlyName == jobVacanciesFriendlyName)
.ThenBy(x => x.FriendlyName == servicesLabourHireFriendlyName)
.ThenBy(x => x.FriendlyName == goodsEquipmentFriendlyName)
.ToList();
This was fast enough on my dev machine, but alas! Deployed to Azure it's very slow. The reason seems to be that this query is making hundreds of dependency calls to the database, I'm pretty sure because of the immediate execution of the Count statements. Although the app and the database are in the same datacenter, the calls add up in a way they didn't on my dev machine (~40s vs < 1s). So what I'd like to do is send this whole thing off to the database, let it crunch, and get it all back in one hit, if it's possible. How do I do this? Also if I'm approaching this whole thing wrong please tell me. This is the biggest bottleneck in my web app so any help to make it more efficient is appreciated. Thank you! (I'm less concerned about web app memory usage than I am about the cumulative effect of all the database calls.)
This is my suggestion to your massive query.
Don't use ToList() inside the inner queries.
Don't use Count() inside the inner queries.
Try to retrieve all the data once without above IEnumerable operations.In other words fetch the data as IQueryable mode.After loading it in to the App's memory,you can create your data model as you wish.This process will give huge performance boost to your app.So try that and let us know.
Update : about Count()
If you have lot of columns on that list, just fetch a 1 column without Count() using projection.After that you can get the count() on your IEnumerable list.In other words on your app's memory after fetching it from the db.
Here's what I've got so far. It's working really well, but I'm still curious if I can do this in one DB trip, not two. That would seem to be complicated by the fact that each repository has its own DBContext. If you guys have any more thoughts I'd be more than happy to upvote you.
var allCategories = categoryRepository
.Categories
.Select(x => new
{
x.CategoryID,
x.FriendlyName,
x.RoutingName,
x.ParentID
})
.ToList();
var allListings = listingRepository
.Listings
.Where(x => x.Lister.Status != Subscription.StatusEnum.Cancelled.ToString())
.Select(x => new
{
x.SelectedCategoryOneID,
x.SelectedCategoryTwoID,
x.SelectedCategoryThreeID,
})
.ToList();
categories =
allCategories
.Where(x => x.ParentID == null)
.Select(a => new CategoryBrowseIndexViewModel
{
CategoryID = a.CategoryID,
FriendlyName = a.FriendlyName,
RoutingName = a.RoutingName,
ListingCount = allListings
.Where(x => x.SelectedCategoryOneID == a.CategoryID)
.Count(),
BrowseCategoriesLevelTwoViewModels =
allCategories
.Where(x => x.ParentID == a.CategoryID)
.Select(b => new BrowseCategoriesLevelTwoViewModel
{
CategoryID = b.CategoryID,
FriendlyName = b.FriendlyName,
RoutingName = b.RoutingName,
ParentRoutingName = a.RoutingName,
ListingCount = allListings
.Where(x => x.SelectedCategoryTwoID == b.CategoryID)
.Count(),
BrowseCategoriesLevelThreeViewModels =
allCategories
.Where(x => x.ParentID == b.CategoryID)
.Select(c => new BrowseCategoriesLevelThreeViewModel
{
CategoryID = c.CategoryID,
FriendlyName = c.FriendlyName,
RoutingName = c.RoutingName,
ParentRoutingName = b.RoutingName,
ParentParentID = a.CategoryID,
ParentParentRoutingName = a.RoutingName,
ListingCount = allListings
.Where(x => x.SelectedCategoryThreeID == c.CategoryID)
.Count()
})
.OrderBy(x => x.FriendlyName)
})
.OrderBy(x => x.FriendlyName)
})
.OrderBy(x => x.FriendlyName == jobVacanciesFriendlyName)
.ThenBy(x => x.FriendlyName == servicesLabourHireFriendlyName)
.ThenBy(x => x.FriendlyName == goodsEquipmentFriendlyName);
I do write the following SQL query in LINQ c#
SELECT max(creation_date) from TRS where approval_status='APPROVED' and transaction_type in ('Sale','PRE')
I tried building below query on a list as follows
var session = txns.Where(a => a.transaction_type.Equals("SALE"))
.Where(a => a.transaction_type.Equals("PRE"))
.Where(a => a.approval_status.Equals("APPROVED"))
.OrderByDescending(a => a.creation_date).Select(a => a.creation_date).FirstOrDefault();
The above query didnt work as I wasn't sure of how to use Max and OR condition in LINQ c#
May I know a better solution?
var session = txns
.Where(a => a.transaction_type.Equals("SALE") || a.transaction_type.Equals("PRE"))
.Where(a => a.approval_status.Equals("APPROVED"))
.Select(a=>a.creation_date).Max();
or
var txtypes=new[]{"SALE","PRE"};
var session = txns
.Where(a => txtypes.Contains(a.transaction_type))
.Where(a => a.approval_status.Equals("APPROVED"))
.Select(a=>a.creation_date).Max();
or
var session = txns
.Where(a => a.transaction_type.Equals("SALE") || a.transaction_type.Equals("PRE"))
.Where(a => a.approval_status.Equals("APPROVED"))
.Max(a=>a.creation_date);
You can use || operator to combine your two conditions or you can use Contains which would generate a query like SELECT IN (....)
var transcationTypes = new[] {"SALE", "PRE"};
var sessions = txns.Where(a => transcationTypes.Contains(a.transaction_type)
&& a.approval_status == "APPROVED")
.Select(a => a.creation_date)
.Max();
Once you have filtered out the results you can use Max to select the maximum value.
I have this C# code that works, but I'd like to be able to choose the Agency if a person is in it all in the query. Is there a way to do that all in the query?
var retVal = new List<Agency>();
var items=_db.Agencies
.Include(x => x.AgencyMembers.Select(y => y.Person))
.Where(w => w.NationId == User.NationId).ToList();
foreach (var agency in items)
{
if(agency.AgencyMembers.Any(c=>c.Person.Id==personId))
retVal.Add(agency);
}
return retVal;
You should be able to just add that predicate to your query.
return _db.Agencies
.Include(x => x.AgencyMembers.Select(y => y.Person))
.Where(w => w.NationId == User.NationId)
.Where(agency => agency.AgencyMembers.Any(c=>c.Person.Id==personId))
.ToList();
Depending what navigation properties you have, you may be able to simplify it by starting from the person.
return _db.People
.Single(p => p.Id == personId)
.Agencies
.Where(w => w.NationId == User.NationId)
.ToList();
You can try this:
var items=_db.Agencies
.Include(x => x.AgencyMembers.Select(y => y.Person))
.Where(agency=> agency.NationId == User.NationId && agency.AgencyMembers.Any(c=>c.Person.Id==personId))
.ToList();