【发布时间】:2021-07-28 06:24:05
【问题描述】:
我有 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;
一个实体可以有多个额外的字段。并非所有实体都包含相同数量的额外字段。因此,需要左连接。可查询的最终结果应导致以下结果:
| Entity Id | Extra field 1 | Extra field 2 | Extra field 3 |
|---|---|---|---|
| 1 | value | value | value |
| 2 | value | 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,我得到了这个工作。但是与此等效的 Dynamic 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
};
【问题讨论】:
-
用 SQL 编写 PIVOT 查询并使用 Dapper 获取数据。容易得多。
-
我确实在尝试创建一个支点。但我更愿意利用我还需要订购和跳过/服用的 linq 功能
标签: c# linq dynamic-linq dynamic-linq-core