I have the following tables:
Customer => CustomerAccount => Account
I have an nHibernate mapped POCO to each of the tables above as well.
I have the following lambda expression in an object which implements IIdentifier<T>
public Expression<Func<ICustomer, bool>> Filter
{
get { return customer => customer.CustomerNumber == _customerNumber; }
}
Now what I'm trying to do is join the Customer => CustomerAccount => Account tables via a QueryOver<Account>
How do I add the above Filter lamdba, which is of type Customer, to filter by the customer number?
ICustomer customer = null;
ICustomerAccount customerAccount = null;
IAccount account = null;
var query = QueryOver(() => account)
.JoinAlias(() => account.CustomerAccounts, () => customerAccount, JoinType.InnerJoin)
.JoinAlias(() => customerAccount.Customer, () => customer, JoinType.InnerJoin)
.Where(() => customerAccount.EndDate == null)
.And(() => account.IsPreferredAccount == true)
.And(() => ?? Want to add the above Filter() lambda some how here);
Thanks,
Kyle
You could try:
var query = QueryOver(() => account)
.JoinAlias(() => account.CustomerAccounts, () => customerAccount, JoinType.InnerJoin)
.JoinAlias(() => customerAccount.Customer, () => customer, JoinType.InnerJoin)
.Where(() => customerAccount.EndDate == null)
.And(() => account.IsPreferredAccount == true)
.And(Restrictions.Where<ICustomer>(Filter));
Related
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);
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()
});
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.
Select Label,
(SELECT COUNT(*) from [CourtSessions] cs where cs.iDCity = Cit.ID) as courts,
(Select COUNT(*) from [Cases] c inner join [CourtSessions] cs ON c.ID = cs.iDCase where cs.iDCity = Cit.ID) as csnatures
FROM Cities Cit
Group by Label, id
I tried this but it doesn't work
var data = db.Cities
.GroupBy(a => a.label)
.Select(g => new
{
city = g.Key,
sessions = db.CourtSessions.Include(p => p.CityTB).Count(o => o.CityTB.label == g.Key),
cases = db.Cases.Join(db.CourtSessions, u => u.ID, ui => ui.iDCase, (u, ui) => new { u, ui }).Count(m => m.ui.CityTB.label == g.Key)
});
Where CityTB is a foreign key
Cases (ID ...)
Cities (ID, Label)
CourtSession (ID, iDCase, iDCity ... CasesTB, CityTB)
I am getting this exception
base {System.Exception} = {"LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[LawbookMVC.Models.CourtSession] Include[CourtSession,City](System.Linq.IQueryable1[LawbookMVC.Models.CourtSession], System.Linq.Expressions.Expression1[System.Func2[LawbookMVC.Mod...
Thanks.
Well i solved it, thanks you all
var dat = db.Cities
.GroupBy(a => new { a.label, a.ID})
.Select(g => new
{
city = g.Key.label,
sessions = db.CourtSessions.Count(o => o.iDCity == g.Key.ID),//,
cases = db.Cases.Join(db.CourtSessions, u => u.ID, ui => ui.iDCase, (u, ui) => new { u, ui }).Count(m => m.ui.CityTB.label == g.Key.label)
});
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>();