C#Linq:使查询语法等于thenInclude的方法语法

问题描述 投票:-1回答:2

我正在尝试将方法语法转换为查询语法。

类结构嵌套如下:

  1. 属性
  2. PropertyParty
  3. 聚会
  4. PartyMailingAddress
  5. PropertyMailingAddress

[似乎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

c# .net entity-framework linq .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();

0
投票
如果您确实需要将其与查询结合使用,则可以执行以下操作:

(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();
© www.soinside.com 2019 - 2024. All rights reserved.