我正在尝试将方法语法转换为查询语法。
类结构嵌套如下:
[似乎MethodSyntax(第一个),与查询语法相比,带来了更少的分组在一起的行,带来了更多的行。
如何将第二个查询语法固定为等效于方法语法?
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();
*如果没有等效项,我可以获取查询结果,并将其分组为类似于“方法语法”的方法吗?
查询语法答案不应包含ThenInclude
当前正在使用Net Core 2.2
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();
(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();
如果您要使用查询语法来查找group by
,则可能会是这样(但对我而言,所有这些工作似乎都不值得)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();