【发布时间】:2020-03-13 21:43:19
【问题描述】:
我正在使用 Linq.Dynamic.Core 为 EF Core 动态创建查询,但我陷入了使用多个主键的联接操作中。使用单个主键表,一切都可以完美运行。
作为第一步,我在标准 Linq 中创建了一个示例查询:
var resultLinq = ctx.Set<Destination>()
.Join(ctx.Set<DestinationCollectionInfo>(),
p => new { A1 = p.Id, A2 = p.DestinationId },
j => new { A1 = j.Id, A2 = j.DestinationId },
(p, j) => new { Description = p.Description, Collection_Descr = j.Description })
.ToList();
然后我把它翻译成Dynamic Linq Core作为模型:
var resultDynamicLinq = ctx.Set<Destination>()
.Join(ctx.Set<DestinationCollectionInfo>(),
"new { it.Id AS A1, it.DestinationId AS A2 }",
"new { Id AS A1, DestinationId AS A2}",
"new { outer.Description AS Description, inner.Description AS Collection_Descr}")
.ToDynamicList();
Linq 查询有效,但 Dynamic Linq 查询返回此异常:
System.InvalidOperationException: The LINQ expression 'DbSet<Destination>
.Join(
outer: DbSet<DestinationCollectionInfo>,
inner: d => new <>f__AnonymousType2<int, int>{
A1 = d.Id,
A2 = d.DestinationId
}
,
outerKeySelector: d0 => new <>f__AnonymousType2<int, int>{
A1 = d0.Id,
A2 = d0.DestinationId
}
,
innerKeySelector: (d, d0) => new TransparentIdentifier<Destination, DestinationCollectionInfo>(
Outer = d,
Inner = d0
))' 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(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我做错了什么还是 Dynamic Linq 有限制?谢谢!
【问题讨论】:
标签: c# linq entity-framework-core