在EF使用中使用Distinct或GroupBy去重,又使用Include提示以下错误:
Include Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are required to generate results on client side. This can happen when trying to correlate on keyless entity or when using 'Distinct' or 'GroupBy' operations without projecting all of the key columns.
经过查询使用以下方法进行解决:
文章结论如下:
1.在仓储执行linq时,Distinct和GroupBy已弃用
2.若要想使用,则需要在仓储执行之后,再使用Distinct
已弃用代码实例:
context.Parents
.Select(p => p.Children
.GroupBy(c => c.School)
.Select(g => g.Key))
context.Parents
.Select(p => p.Children
.Select(c => c.School)
.Distinct())
新使用方法:
context.Parents
.Select(p => p.Children.Select(c => c.School))
.ToList()
.Select(x => x.GroupBy(c => c).Select(g => g.Key))
context.Parents
.Select(p => p.Children.Select(c => c.School))
.ToList()
.Select(x => x.Distinct())
本文探讨了在EF Core中使用Distinct和GroupBy遇到的问题及解决方案,特别是当结合Include使用时出现的错误,并提供了新的代码实现方法。

876

被折叠的 条评论
为什么被折叠?



