I am trying to convert a Method Syntax to Query Syntax.
Class Structure is nested like this:
It seems MethodSyntax (first one), is bringing less rows grouped together compared to Query Syntax, bringing more rows.
How do I fix second Query syntax to be equivalent to Method Syntax?
var result = db.Property.Include(pm => pm.PropertyParty)
.Include(pm => pm.PropertyParty)
.ThenInclude(x => x.Party)
.ThenInclude(x => x.PartyMailingAddress)
.ThenInclude(x => x.PropertyMailingAddress)
.ToList();
var testingResult = (from property in db.Property
join propertyParty in db.PropertyParty
on property.PropertyId equals propertyParty.PropertyId
join party in db.Party
on propertyParty.PartyId equals party.PartyId
join partyMailingAddress in db.PartyMailingAddress
on party.PartyId equals partyMailingAddress.PartyId
join propertyMailingAddress in db.PropertyMailingAddress
on partyMailingAddress.PartyMailingAddressId equals propertyMailingAddress.PartyMailingAddressId
select property).ToList();
*If there is no equivalent, could I grab the query results, and group them to be similar to Method Syntax?
The Query syntax answer should Not contain ThenInclude
Currently using Net Core 2.2
There is not special LINQ syntax for Include. So the equivilent is not a bunch of joins. It's
var q = from p in db.Property
.Include(pm => pm.PropertyParty)
.Include(pm => pm.PropertyParty)
.ThenInclude(x => x.Party)
.ThenInclude(x => x.PartyMailingAddress)
.ThenInclude(x => x.PropertyMailingAddress)
select p;
var testingReqult = p.ToList();
The reason why you're seeing more rows on the Query Syntax is because there you're doing joins, while the Method one is doing includes which just loads the related entities (check this ).
If you really need to combine it with a query you could do something like this:
(from property in db.Property
select property)
.Include(pm => pm.PropertyParty)
.Include(pm => pm.PropertyParty)
.ThenInclude(x => x.Party)
.ThenInclude(x => x.PartyMailingAddress)
.ThenInclude(x => x.PropertyMailingAddress)
.ToList();
If you're looking for a group by
because you want to stick with the query syntax, it would be something like this (but to me all this work doesn't seem to be worth it)
var testingResult = (from property in db.Property
join propertyParty in db.PropertyParty
on property.PropertyId equals propertyParty.PropertyId
join party in db.Party
on propertyParty.PartyId equals party.PartyId
join partyMailingAddress in db.PartyMailingAddress
on party.PartyId equals partyMailingAddress.PartyId
join propertyMailingAddress in db.PropertyMailingAddress
on partyMailingAddress.PartyMailingAddressId equals propertyMailingAddress.PartyMailingAddressId
group property by new { property.Field1, x.Field2, ... /* Group by all the property fields */ } into grouped
select new Property
{
Field1 = grouped.Key.Field1,
Field2 = grouped.Key.Field2,
... /* Manually populate all the properties */
PropertyParties = grouped.Select(x => x.PropertyParties),
... /* Manually populate all the related entities */
}).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.