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