Since:
"Eager loading a collection navigation in a single query may cause
performance issues."
see: Source
And it is advise to use split queries with include. I wonder if instead of include in the query bellow:
var task = await context.Tasks
.Include(x => x.TaskDependencies)
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
I should do this:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= context.TaskDependencies
.Where(y => y.TaskId == x.Id).ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
Anyone as any info regarding this? about performance, etc..
Regards
Both queries should have the same performance and SQL. Note that Include followed by Select is ignored by EF Core.
So, most comfortable query is:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies = x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
Related
I have pretty simple LINQ expression
IQueryable<FreeBetDTO> records = UnitOfWork.FreeBets
.Include(f => f.FreeBetCategories)
.Include(f => f.FreeBetCards)
.Where(f => f.FreeBetCards.Any(cards => cards.UserId == request.UserId))
.Select(f => new FreeBetDTO
{
FreeBetId = f.FreeBetId
LineCategories = f.FreeBetCategories
.GroupBy(g => new { g.LineCategoryID, g.Title })
.Select(c =>
new LineCategoryDTO
{
LineCategoryID = c.Key.LineCategoryID,
Title = c.Key.Title
}).AsEnumerable()
});
When I am executing it I catch the error:
System.InvalidOperationException: Unable to translate collection subquery in projection since it uses 'Distinct' or 'Group By' operations and doesn't project key columns of all of it's tables which are required to generate results on client side. Missing column: t.ID. Either add column(s) to the projection or rewrite query to not use 'GroupBy'/'Distinct' operation.
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.
....
The problem is here .GroupBy(g => new { g.LineCategoryID, g.Title }). If I don't group records, the error disappears.
I was trying a lot of cases with GroupBy() and Distinct(). But can't understand why this is happening. Because I just need grouping like this.
Error message says that you have to include Id column in projection. But you can't do that with GroupBy. So rewrite query into two steps (removed not needed includes):
var rawRecords = UnitOfWork.FreeBets
.Where(f => f.FreeBetCards.Any(cards => cards.UserId == request.UserId))
.Select(f => new
{
FreeBetId = f.FreeBetId
LineCategories = f.FreeBetCategories.Select(c => new { c.Id, c.LineCategoryID, c.Title })
.ToList()
})
.AsEnumerable();
var records = rawRecords
.Select(f => new FreeBetDTO
{
FreeBetId = f.FreeBetId
LineCategories = f.LineCategories.GroupBy(g => new { g.LineCategoryID, g.Title })
.Select(c =>
new LineCategoryDTO
{
LineCategoryID = c.Key.LineCategoryID,
Title = c.Key.Title
})
});
Similar query, but more optimal:
var query =
from f in UnitOfWork.FreeBets
from c in f.FreeBetCards
where f.FreeBetCards.Any(cards => cards.UserId == request.UserId)
select new { f.FreeBetId, c.LineCategoryID, c.Title };
query = query.Distinct();
var records = query.AsEnumerable()
.GroupBy(f => f.FreeBetId)
.Select(g => new FreeBetDTO
{
FreeBetId = g.Key
LineCategories = g.Select(c =>
new LineCategoryDTO
{
LineCategoryID = c.LineCategoryID,
Title = c.Title
})
.AsEnumerable()
});
As it's described in title I'm getting somehow like list which contains oblivously list of strings?
How come?
Here is my query:
var productChangesUpdate = await _dbContext.ProductUpdates.Where(x => x.ProductId == ProductId)
.Select(x => new ProductChangeDto
{
Id = x.Id,
Title = x.Title,
Price = x.Price,
Date = x.CreatedDate,
ResponsiblePerson = x.ProductDeploy.ProductDeployResponsiblePersons.Select(x => x.User.FirstName + x.User.LastName).ToString()
}).ToListAsync(cancellationToken);
Issue is that ResponsiblePerson value is System.Collections.Generic.List`1[System.String] instead of Full name..
What's wrong here?
Thanks
You are returning a List. You must use something similar to FirstOrDefaultAsync() or SingleOrDefaultAsync() in your case.
var productChangesUpdate = await _dbContext.ProductUpdates.Where(x => x.ProductId == ProductId)
.Select(x => new ProductChangeDto
{
Id = x.Id,
Title = x.Title,
Price = x.Price,
Date = x.CreatedDate,
ResponsiblePerson = x.ProductDeploy.ProductDeployResponsiblePersons.Select(x => x.User.FirstName + x.User.LastName).ToString()
}).FirstOrDefaultAsync(cancellationToken);
Why am I getting only one entry in DownTimeDetails list even though in Data we have 3 entries.
VehicleEventDetails Res = dbEntity.DownTimeHeaders
.Join(dbEntity.DownTimeDetails, dth => dth.DownTimeHeaderID, dtd => dtd.DownTimeHeaderID, (dth, dtd) => new { dth, dtd })
.Where(x => x.dth.DownTimeHeaderID == 42)
.GroupBy(gx => gx.dtd.DownTimeDetailID)
.Select(t => new VehicleEventDetails()
{
BookingId = t.Select(a => a.dth.BookingId).FirstOrDefault(),
DownTimeDetails = t.Select(ab => new DownTimeDetails
{
LocalDTStartTime = (DateTime)ab.dtd.LocalDTStartTime,
LocalDTEndTime = (DateTime)ab.dtd.LocalDTEndTime,
CalculatedEventDTReason = ab.dtd.CalculatedEventDTReason,
CalculatedEventDTInMinutes = (int)ab.dtd.CalculatedEventDT,
}).ToList()
}).FirstOrDefault();
You are looking for something like this:
VehicleEventDetails Res = dbEntity.DownTimeHeaders
.Where(x => x.DownTimeHeaderID == 42)
.Select(x => new VehicleEventDetails
{
BookingId = x.BookingId,
DownTimeDetails = x.DownTimeDetails
.Select(dtd=> new DownTimeDetails
{
LocalDTStartTime = (DateTime)dtd.LocalDTStartTime,
LocalDTEndTime = (DateTime)dtd.LocalDTEndTime,
CalculatedEventDTReason = dtd.CalculatedEventDTReason,
CalculatedEventDTInMinutes = (int)dtd.CalculatedEventDT,
})
.ToList()
})
.FirstOrDefault();
Notes:
Using .Join is an anti-Entity Framework pattern. Always try to use navigation properties, they exist for a reason.
Don't use .GroupBy unless you actually need a group. You don't want any grouping in this query.
As a general note, try not to make the expression variable names so confusing.
I have two collections
var campaigns = new List<Campaigns>();
IEnumerable<CampaignsDb> campaignsFromDB = db.Campaigns
.Where(c => (c.IsDeleted == false))
.OrderBy(c => c.ScheduleTime)
.ToArray();
Next I'm filling one collection from another using foreach() :
foreach (var campaign in campaignsFromDB)
{
campaigns.Add(new Campaigns { CampaignID = campaign.CampaignID, OwnerID = campaign.CreatedBy, AccountID = campaign.AccountID });
}
Can I use Select() linq method instead of foreach loop ?
Unless you need the original empty list to start with (e.g. to add some other campaigns first), you can use:
var campaigns = campaignsFromDB
.Select(c => new Campaigns
{
CampaignID = c.CampaignID,
OwnerID = c.CreatedBy,
AccountID = c.AccountID
})
.ToList();
However, that will still have fetched the complete campaign information from the database in order to populate campaignsFromDB. That's fine if you need that array for some other reason, but if not, you could make it more efficient by putting the projection into the query:
var campaigns = db.Campaigns
.Where(c => !c.IsDeleted)
.OrderBy(c => c.ScheduleTime)
.Select(c => new Campaigns
{
CampaignID = c.CampaignID,
OwnerID = c.CreatedBy,
AccountID = c.AccountID
})
.ToList();
Yes. Just
var campaigns = db.Campaigns
.Where(c => (c.IsDeleted == false))
.OrderBy(c => c.ScheduleTime)
.Select(c => new Campaigns { CampaignID = c.CampaignID, OwnerID = c.CreatedBy, AccountID = c.AccountID })
.ToList();
or if you need the source set from the database too
var campaignsFromDB = db.Campaigns
.Where(c => (c.IsDeleted == false))
.OrderBy(c => c.ScheduleTime)
.ToArray();
var campaigns = campaignsFromDB
.Select(c => new Campaigns { CampaignID = c.CampaignID, OwnerID = c.CreatedBy, AccountID = c.AccountID })
.ToList();
Yes, like this:
var campaigns = db.Campaigns
.Where(c => !c.IsDeleted)
.OrderBy(c => c.ScheduleTime)
.Select(c => new Campaigns
{
CampaignID = c.CampaignID,
OwnerID = c.CreatedBy,
AccountID = c.AccountID
})
.ToList();
The Person class has a association by Identity class (one-to-one) FirstName and LastName are a property of Person class also Sex and BirthDate are a property of Identity class.
I have a sql query as the following examples:
select FirstName,LastName,Identity.Sex,Identity.BirthDate from Person_Person as Person
inner join Person_Identity as Identity on Person.Id = Identity.Person_id_fk
WHERE FirstName like '%jack%' and LastName like '%smit%'
I convert it into QueyOver.
var q = SessionInstance.QueryOver<Person>();
if (!String.IsNullOrEmpty(searchPersonDto.FirstName)) //necessary
q = q.Where(p => p.FirstName.IsLike(searchPersonDto.FirstName, MatchMode.Anywhere));
if (!String.IsNullOrEmpty(searchPersonDto.LastName)) //necessary
q = q.Where(p => p.LastName.IsLike(searchPersonDto.LastName, MatchMode.Anywhere));
Person aliasPerson = null;
q = q.SelectList(list => list
.Select(p => p.Id).WithAlias(() => aliasPerson.Id)
.Select(p => p.FirstName).WithAlias(() => aliasPerson.FirstName)
.Select(p => p.LastName).WithAlias(() => aliasPerson.LastName)
.Select(p => p.Identity.Sex).WithAlias(() => aliasPerson.Identity.Sex)
.Select(p => p.Identity.BirthDate).WithAlias(() => aliasPerson.Identity.BirthDate))
.TransformUsing(Transformers.AliasToBean<Person>());
q.List<Person>();
But join in this query is not correct. It throw a exceotion by this message :
could not resolve property: Identity.Sex of: Domain.Entities.Person
How I should join Identity by Person?
Updated : Add the similar linq query
var q = SessionInstance.Query<Person>()
.Where(p => p.FirstName == searchPersonDto.FirstName)
.Select(p => new Person(p.Id)
{
FirstName = p.FirstName,
LastName = p.LastName,
Identity = new Identity()
{
Sex = p.PersonIdentity.Sex,
BirthDate = p.Identity.BirthDate
}
}).ToList<Person>();
I need to a query by QueryOver similar to above query by Linq.
Update2: not pretty but here goes
var results = q
.JoinAlias(p => p.Identity, () => identityAlias)
.SelectList(list => list
.Select(p => p.Id)
.Select(p => p.FirstName)
.Select(p => p.LastName)
.Select(p => identityAlias.Sex)
.Select(p => identityAlias.BirthDate)
.List<object[]>()
.Select(values => new Person((int)values[0])
{
FirstName = (string)values[1],
LastName = (string)values[2],
Identity = new Identity()
{
Sex = (string)values[3],
BirthDate = (DateTime)values[4],
}
})
.ToList<Person>();
Update: from your comments i would say, this is what you need.
code to fill a PersonDto
PersonDTO aliasDTO = null;
q = q
.JoinAlias(p => p.Identity, () => identityAlias)
.SelectList(list => list
.Select(p => p.Id).WithAlias(() => aliasDTO.Id)
.Select(p => p.FirstName).WithAlias(() => aliasDTO.FirstName)
.Select(p => p.LastName).WithAlias(() => aliasDTO.LastName)
.Select(p => identityAlias.Sex).WithAlias(() => aliasDTO.Sex)
.Select(p => identityAlias.BirthDate).WithAlias(() => aliasDTO.BirthDate))
.TransformUsing(Transformers.AliasToBean<PersonDTO>())
.List<PersonDTO>();
Orginal Answer:
q.JoinAlias(p => p.Identity, () => identityAlias)
// and later
.Select(p => identityAlias.Sex)
Update: in the code posted the AliasToBeanTransformer is not needed at all
var q = SessionInstance.QueryOver<Person>();
if (!String.IsNullOrEmpty(searchPersonDto.FirstName)) //necessary
q = q.Where(p => p.FirstName.IsLike(searchPersonDto.FirstName, MatchMode.Anywhere));
if (!String.IsNullOrEmpty(searchPersonDto.LastName)) //necessary
q = q.Where(p => p.LastName.IsLike(searchPersonDto.LastName, MatchMode.Anywhere));
var results = q.Fetch(p => p.Identity).Eager
.List<Person>();