【问题标题】:How to Make C# Linq Query Syntax Equivalent to Method Syntax for ThenInclude?如何使 C# Linq 查询语法等效于 ThenInclude 的方法语法?
【发布时间】:2020-05-27 08:18:52
【问题描述】:

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

类结构嵌套如下:

  1. 财产
  2. 物业方
  3. 聚会
  4. 派对邮寄地址
  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

【问题讨论】:

  • 请不要在问题标题中使用标签。 tagging help page 强烈建议不要这样做:“你应该在标题中使用标签的唯一情况是它们与标题的对话语气有机。”
  • 首先,这个网站的规则会随着时间而改变,所以当时可能没有标题中的标签不是标签指南的一部分。也许这些问题只是从网上溜走了——很多都是这样。将您对该网站的使用与实际的官方指南相矛盾并用旧问题为其辩护似乎类似于要求官员不要给您一张超速罚单,因为大约 10 年前速度限制曾经更高。
  • 这是一篇关于标题标签的元帖子:meta.stackexchange.com/q/19190/686592
  • 换个角度看这个:这对你有什么好处?如果有人有兴趣回答有关 C# 或 LINQ 的问题,他们不会对 C# 或 LINQ 进行文本搜索。和我一样,他们会打开C# tag page,以便他们可以看到标记为 C# 的新问题。我就是这样找到你的问题的。如果您是这么想的话,它根本不会增加您的知名度。

标签: c# .net entity-framework linq .net-core


【解决方案1】:

Include 没有特殊的 LINQ 语法。所以等价不是一堆连接。这是

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();

【讨论】:

  • 你是说没有等价物吗?您刚刚将我的第一个放在上面,是否没有 linq to sql 替代方案?谢谢我无法获取 linq to sql 结果,然后将它们分组压缩??
  • 我无法抓取 linq to sql 结果,然后分组压缩??
【解决方案2】:

您在查询语法中看到更多行的原因是因为您正在执行连接,而方法一正在执行包含仅加载相关实体的操作(检查 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();

如果您正在寻找 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();

【讨论】:

  • 我试图避免使用 theninclude,这不会回答问题
  • 我想说使用查询语法没有与Include 等效的值。我很确定做 Includes 的唯一方法是调用 Include() 这样的方法。我试图说明如何将两者结合起来,以防查询更复杂。
  • 我无法抓取 linq to sql 结果,然后分组压缩??
  • @Artportraitdesign1 刚刚编辑了我的答案,这就是使用 group by 的方法,我只是使用方法 one 虽然 tbh。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-16
相关资源
最近更新 更多