Doing Eager Loading and Projection in Linq to Entities - c#

This is a spin off from another question I posted a few days ago that was successfully answered but it really didn't get into the underlying issue I was having but wasn't able to express fully in my original query.
I have a table Product. Product is related to ProductDescription in a one-to-many relationship. ProductDescription can have more than one row for each product. It will have multiple rows if there are multiple translations for the description of the product. Product has a many-to-one relationship with Language. Language has a language code (en, es, etc.) as well as a LanguageId (found in ProductDescription as well).
I want to give my users the ability to request a product and further tell the application to only return descriptions for that product in a specific language.
The problem I'm having is that I understand I need to use projection to accomplish the task in the 3rd paragraph of this question. Something like this:
var query = inventoryRepository.Products
.Where(wherePredicate)
.Select( a=> new Product
{
ProductDescriptions = inventoryRepository.ObjectContext.ProductDescriptions
.Where(a => a.Languages.AlternateCode.Equals("en", StringComparison.CurrentCultureIgnoreCase))
});
However I have about 15 properties in the Products table as well as 4 other child tables of Products that need to be loaded for the result set in addition to just the languages piece. Is there any way for me to do eager loading AND projection so that I don't have to go through and map all of these properties and children objects manually? Here is where my code stands right now:
var query = inventoryRepository.ObjectSet
.Include("ProductDescriptions")
.Include("ProductAllowedWarehouses")
.Include("ProductToCategories")
.Include("PriceLevels")
.Include("AttachmentAssociations.Attachment").AsExpandable()
.Where(wherePredicate);
No Select necessary which is really nice. Once I change ProductDescriptions to a projection I add a Select and then I don't get any of the other properties / children populated for free.

I would read this blog about projections+eager loading. You are going to run into issues when it comes to the eager loading. Recommendation is to use a .Any and perform a sub select.
Why can't you just add a filter/where on ProductDescriptions?
var query = inventoryRepository.ObjectSet
.Include("ProductDescriptions")
.Include("ProductAllowedWarehouses")
.Include("ProductToCategories")
.Include("PriceLevels")
.Include("AttachmentAssociations.Attachment").AsExpandable()
.Where(wherePredicate)
.Where(a=>a.ProductDescriptions.Languages.AlternateCode.Equals("en", StringComparison.CurrentCultureIgnoreCase);

Related

Entity framework the opposite of include

We are working on a Project in which there are many linq queries are not optimized, because as they started on the project they used the property virtual for all of their models.
My task is to optimize the max number of the queries, in order to enhance the app performance.
The problem is if I use the Include function and delete all virtual properties from the model, lot of things stop working and the number of affected functions is huge.
So I thought if I can find some thing resemble to "exclude" to exclude the unnecessary sub queries in some cases.
(with the assumption of your result set implements ienumerable)
My first choice would be:
ListMain.Except(ItemsToExclude);
Or, I would go with (not) "Contains" as follows and have check in-between to exclude the records. This may not be the best way out there but I could work.
!ListMain.Contains(ItemsToExclude)
I don't know if I got the question right, but to avoid loading certain attributes or related objects, you can make an additional Select() including only what's needed.
Example:
A simple ToList() will bring the entire object from the table:
var resultList = await dbContext.ABTests.AsNoTracking().ToListAsync();
It will derives in the query:
SELECT [a].[Id], [a].[AssignedUsers], [a].[EndDate], [a].[Groups], [a].[Json], [a].[MaxUsers], [a].[Name], [a].[NextGroup], [a].[StartDate]
FROM [ABTests] AS [a]
(which includes all mapped fields of the ABTest object)
To avoid fetching all, you can do as follow:
var resultList = await dbContext.ABTests.AsNoTracking().Select(x => new ABTest
{
Id = x.Id,
Name = x.Name
}).ToListAsync();
(supposing that you only wan the fields Id and Name to be eagerly loaded)
The resulting SQL Query will be:
SELECT [a].[Id], [a].[Name]
FROM [ABTests] AS [a]

Linq Foreign Key Select

I have two tables with a one (Articles) to many (Details) relationship. Details may not contain any data for the particular Article entry.
Articles: Id, Title, Numb (PK), Name
Details: Id (PK), Person, Numb (FK), Name
In the Entity Framework, there are the appropriate Navigation properties and it shows the correct One:Many relationship.
What I want to do is get all Articles that match the end user's query (by 'Name') as well as all, if any, data from the Details table (Id, Person, Numb, Name).
What I'm stuck on is right now I can query Articles just fine (var article = db.Articles.Where(b => b.Name.Equals(name));), but while the result does include a HashSet for Details.Numb on each row of Articles, there is no data in that HashSet. There are appropriate corresponding entries in the database for Article.Numb => Details.Numb.
Actually there is two ways to achieve this.
Enable Lazy Loading.
Call Include method as other answers says.
Using Lazy Loading see msdn article for more detail.
db.ContextOptions.LazyLoadingEnabled = true;
Using Include method
var article = db.db.Articles.Include("Details").Where(b => b.Name.Equals(name))).FirstOrDefault();
You need to tell EF to include the details in the result set after the query is executed (and connection closes):
var article = db.Articles
.Include("Details")
.Where(b => b.Name.Equals(name))
.FirstOrDefault();
Use .Include() on the navigation property, it will bring the entire inner object in the query result. It's only automatic if you filter or select items from the inner object, otherwise you have to manually request an include.
Example:
var allProducts = _db.Products.Include(d => d.Producer).ToList();
Always go with Include instead of lazy loading if you're not sure.

Entity Framework include vs where

My database structure is this: an OptiUser belongs to multiple UserGroups through the IdentityMap table, which is a matching table (many to many) with some additional properties attached to it. Each UserGroup has multiple OptiDashboards.
I have a GUID string which identifies a particular user (wlid in this code). I want to get an IEnumerable of all of the OptiDashboards for the user identified by wlid.
Which of these two Linq-to-Entities queries is the most efficient? Do they run the same way on the back-end?
Also, can I shorten option 2's Include statements to just .Include("IdentityMaps.UserGroup.OptiDashboards")?
using (OptiEntities db = new OptiEntities())
{
// option 1
IEnumerable<OptiDashboard> dashboards = db.OptiDashboards
.Where(d => d.UserGroups
.Any(u => u.IdentityMaps
.Any(i => i.OptiUser.WinLiveIDToken == wlid)));
// option 2
OptiUser user = db.OptiUsers
.Include("IdentityMaps")
.Include("IdentityMaps.UserGroup")
.Include("IdentityMaps.UserGroup.OptiDashboards")
.Where(r => r.WinLiveIDToken == wlid).FirstOrDefault();
// then I would get the dashboards through user.IdentityMaps.UserGroup.OptiDashboards
// (through foreach loops...)
}
You may be misunderstanding what the Include function actually does. Option 1 is purely a query syntax which has no effect on what is returned by the entity framework. Option 2, with the Include function instructs the entity framework to Eagerly Fetch the related rows from the database when returns the results of the query.
So option 1 will result in some joins, but the "select" part of the query will be restricted to the OptiDashboards table.
Option 2 will result in joins as well, but in this case it will be returning the results from all the included tables, which obviously is going to introduce more of a performance hit. But at the same time, the results will include all the related entities you need, avoiding the [possible] need for more round-trips to the database.
I think the Include will render as joins an you will the able to access the data from those tables in you user object (Eager Loading the properties).
The Any query will render as exists and not load the user object with info from the other tables.
For best performance if you don't need the additional info use the Any query
As has already been pointed out, the first option would almost certainly perform better, simply because it would be retrieving less information. Besides that, I wanted to point out that you could also write the query this way:
var dashboards =
from u in db.OptiUsers where u.WinLiveIDToken == wlid
from im in u.IdentityMaps
from d in im.UserGroup.OptiDashboards
select d;
I would expect the above to perform similarly to the first option, but you may (or may not) prefer the above form.

Data Loading Strategy/Syntax in EF4

Long time lurker, first time posting, and newly learning EF4 and MVC3.
I need help making sure I'm using the correct data loading strategy in this case as well as some help finalizing some details of the query. I'm currently using the eager loading approach outlined here for somewhat of a "dashboard" view that requires a small amount of data from about 10 tables (all have FK relationships).
var query = from l in db.Leagues
.Include("Sport")
.Include("LeagueContacts")
.Include("LeagueContacts.User")
.Include("LeagueContacts.User.UserContactDatas")
.Include("LeagueEvents")
.Include("LeagueEvents.Event")
.Include("Seasons")
.Include("Seasons.Divisions")
.Include("Seasons.Divisions.Teams")
.Where(l => l.URLPart.Equals(leagueName))
select (l);
model = (Models.League) query.First();
However, I need to do some additional filtering, sorting, and shaping of the data that I haven't been able to work out. Here are my chief needs/concerns from this point:
Several child objects still need additional filtering but I haven't been able to figure out the syntax or best approach yet. Example: "TOP 3 LeagueEvents.Event WHERE StartDate >= getdate() ORDER BY LeagueEvents.Event.StartDate"
I need to sort some of the fields. Examples: ORDERBY Seasons.StartDate, LeagueEvents.Event.StartDate, and LeagueContacts.User.SortOrder, etc.
I'm already very concerned about the overall size of the SQL generated by this query and the number of joins and am thinking that I may need a different data loading approach alltogether.(Explicit loading? Multiple QueryObjects? POCO?)
Any input, direction, or advice on how to resolve these remaining needs as well as ensuring the best performance is greatly appreciated.
Your concern about size of the query and size of the result set are tangible.
As #BrokenGlass mentioned EF doesn't allow you doing filtering or ordering on includes. If you want to order or filter relations you must use projection either to anonymous type or custom (non mapped) type:
var query = db.Leagues
.Where(l => l.URLPart.Equals(leagueName))
.Select(l => new
{
League = l,
Events = l.LeagueEvents.Where(...)
.OrderBy(...)
.Take(3)
.Select(e => e.Event)
...
});
Unfortunately EF doesn't allow to selectively load related entities using its navigation properties, it will always load all Foos if you specify Include("Foo").
You will have to do a join on each of the related entities using your Where() clauses as filters where they apply.

Can I improve read only perfomance using View on my DataBase and EF?

I use SQL 2008 Linq and Entity Framework 4.
In my DataBase I have a table called CmsContents.
This table has many fields like Title, CategoryId and some pretty heavy like HtmlCode.
In many part of my Front end I need display only some field of my table CmsContents like example only the Title and CategoryId in descent order.
Question:
Can I improve read only performance using a partial View for my Table CmsContents (that exclude the filed: HtmlCode)?
So I would map this View to my Model in EF and query with Linq to get the desidred result in the front-end.
My aim is to improve performance and avoiding working with AnonymousType in my Linq to Entity queries.
Let me know what do you think.
Thanks for your help on this.
You can select only the fields you require, that way you would not need to load un-necessary data. It does not have to be anonymous.
Example:
var result = from c in dbContext.CmsContents select new CmsContent
{Title = c.Title, CategoryId = c.CategoryId}
Edit: please try this on your code, this should work if your .CmsContents is IEnumerables, which I think it is.
var contents = context.CmsGroupsTypes
.Single(g => g.GroupTypeId == 1)
.CmsContents
.Select(cms=>new CmsContent{Title = cms.Title, CategoryId = cms.CategoryId})

Categories

Resources