使用 Dynamic.Linq 在非通用可查询的多个属性上左连接可查询

时间:2021-07-28 06:24:05

标签: c# linq dynamic-linq dynamic-linq-core

我有 2 个包含以下实体的可查询对象:

class Entity
{
    int Id { get;set; }
}

class ExtraField
{
   int EntityId { get;set; }
   string Key { get;set; }
   string Value {get;set; }
}

产生 2 个查询

IQueryable entities;
IQueryable extraFields;

一个实体可以有多个额外的字段。并非所有实体都包含相同数量的额外字段。因此,需要左连接。可查询的最终结果应导致以下结果:

<头>
实体 ID 额外字段 1 额外字段 2 额外字段 3
1 价值 价值 价值
2 价值 NULL NULL
3 NULL NULL NULL

在 SQL 中,我想创建某种 PIVOT 来创建上面的结果。但是,我想用 linq 来实现这一点。

因为一个实体可以有 x 个额外字段,所以我需要在额外字段表上进行 x 个连接。因为该字段并不总是存在,所以我需要一个 LEFT 连接。

我在 stackoverflow 和 Dynamic Linq 文档上花了几个小时,但无法找到有关如何使用带有字符串语法的动态 linq 构建查询的答案。

我走了这么远:

entities.GroupJoin(extraFields, "Id", "EntityId", "new(outer.Id as Id, inner as ExtraFields)").SelectMany("ExtraFields.DefaultIfEmpty()", "new( what do i need to put here??  )");

使用通用的非动态 linq,我可以正常工作。但是动态 Linq 相当于什么?

var result = from entity in entities
             from extraField in extraFields.Where(ef => ef.EntityId == entity.Id && ef.Key = "ExtraField1").DefaultIfEmpty()
             select new
             {
                 EntityId = entity.Id,
                 ExtraField = extraField.Value
             };

1 个答案:

答案 0 :(得分:0)

我猜你正在寻找如何加入。不清楚你希望看到什么

示例:

void Main()
{
    var entities = new List<Entity>() { new Entity() {Id = 1}};
    var extraFields = new List<ExtraField>() { 
        new ExtraField() {EntityId = 1, Key = "ExtraField1", Value="a"},
        new ExtraField() {EntityId = 1, Key = "ExtraField2", Value="b"}
        };
    
    var result = from entity in entities
                 from extraField in extraFields.Where(ef => ef.EntityId == entity.Id && ef.Key == "ExtraField1").DefaultIfEmpty()
                 select new
                 {
                     EntityId = entity.Id,
                     ExtraField = extraField.Value
                 };
                result.Dump("original");
    
    entities
        .GroupJoin(
                extraFields, 
                en => en.Id,
                ext => ext.EntityId,
                (en, ext) => new { entities = en, ExtraField = ext }
            )
            .Dump("join");
            
                
}

public class Entity
{
    public int Id { get; set; }
}

public class ExtraField
{
    public int EntityId { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}


enter image description here

相关问题