【问题标题】:Join using multiple values in Linq Dynamic Core在 Linq Dynamic Core 中使用多个值加入
【发布时间】: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


    【解决方案1】:

    你没有做错任何事。但看起来 EF Core 有一些不同于“默认值”的特定查询表达式要求,因此您应该使用具有 ParsingConfig config 参数并传递 ParsingConfig.DefaultEFCore21 的动态 LINQ 方法重载,例如

    .Join(ParsingConfig.DefaultEFCore21, ctx.Set<DestinationCollectionInfo>(),
    

    没有config 的重载只传递ParsingConfig.Default。理想情况下,ParsingConfig.Default 属性是可设置的,因此可以将其设置一次为ParsingConfig.DefaultEFCore21,然后使用常规重载。但不幸的是,它是只读的,因此如开头所述,您必须明确传递该参数。

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 2021-08-22
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多