【发布时间】:2020-04-07 14:28:37
【问题描述】:
await this.dbContext
.UserTeams
.Where(ut =>
teamMembers.Any(tm => tm.UserId == ut.UserId
&& ut.TeamId == tm.TeamId))
.ToListAsync();
这里的 teamMember 是一个简单的列表,其中包含分组的 UserIds 和 TeamIds。如果我使用 Contains() 但 UserId 和 TeamId 是这里的复合键,这将有效。
这是一个相当简单的查询,无法翻译。
错误:
System.InvalidOperationException: The LINQ expression 'DbSet<UserTeam>
.Where(u => __teamMembers_0
.Any(tm => u.TeamId == tm.TeamId && u.UserId == tm.UserId))'
could not be translated.
Either rewrite the query in a form that can
be translated, or switch to client evaluation explicitly by inserting
a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or
ToListAsync()
【问题讨论】:
-
teamMembers是什么? -
这从未奏效(EF 或 EF Core),主要是因为没有这样的 SQL 构造(
IN不支持复合值)。 -
SP 无济于事,除非你能通过 TVP。有关不同选项,请参阅此答案 stackoverflow.com/questions/26198860/…。它适用于 EF6,但同样适用于 EF Core。
-
@MujahidDaudKhan TVP 代表表值参数,而不是元组。
-
一种选择是将这对 Guid 值转换为字符串表示形式,然后对其进行搜索。不利的一面是您将无法利用这些列上的任何索引。如果您的桌子不是很大,这可能是一个可行的选择。
标签: c# linq ef-core-3.1