I have 2 queryables containing the following entities:
class Entity
{
int Id { get;set; }
}
class ExtraField
{
int EntityId { get;set; }
string Key { get;set; }
string Value {get;set; }
}
Resulting in 2 queryables
IQueryable entities;
IQueryable extraFields;
An entity can have multiple extra fields. Not all entities contain the same number of extra fields. Because of that, a left join is required. The end result of the queryable should lead the following result:
Entity Id | Extra field 1 | Extra field 2 | Extra field 3 |
---|---|---|---|
1 | value | value | value |
2 | value | NULL | NULL |
3 | NULL | NULL | NULL |
In SQL, I would like to create some kind of PIVOT to create the result above. However, I would like to achieve this with linq.
Because an entity can have x extra fields, I would need x number of joins on the extra fields table. Because the field will be not always there, i need a LEFT join.
I have spent some hours on stackoverflow and the Dynamic Linq documentation, but was not able to find an answer on how to build up the query using dynamic linq with string syntax.
I came this far:
entities.GroupJoin(extraFields, "Id", "EntityId", "new(outer.Id as Id, inner as ExtraFields)").SelectMany("ExtraFields.DefaultIfEmpty()", "new( what do i need to put here?? )");
With a generic non-dynamic linq i got this working. But what is the Dynamic Linq equivalent of this?
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
};
A possible solution would be to split the query in two parts:
Result: