Stefan Dantchev
Stefan Dantchev

Reputation: 1

IQueryable Concat throws Unable to translate set operation after client projection has been applied

var q1 = _diagnosisDataRepository
    .Entities
    .Where(ddi => !ddi.DataDefinition.Instances.Any() && ddi.DataDefinition.DataStorageId == id)
    .Where(did => did.DiagnosisDefinition.Instances.Any())
    .Select((DiagnosisDefinitionIdsDataDefinitionId ddi) => new DataDefinitionCustom
    {
        DataDefinitionId = ddi.DataDefinition.Id,
        Instance = ddi.DiagnosisDefinition.Instances.First(),
    });

var q2 = _dataDefinitionRepository
    .Entities
    .Where(dd => dd.DataStorageId == id && dd.Instances != null && dd.Instances.Any())
    .Select((DataDefinition dd) => new DataDefinitionCustom
    {
        DataDefinitionId = dd.Id,
        Instance = dd.Instances.First() 
    });

var q3 = q1.Concat(q2);

var t1 = q1.ToQueryString();
var t2 = q2.ToQueryString();

var q4 = q3.ToArray();

This throws an exception

Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select' call..

On the other hand when I get the t1 and t2 sql for the requests and execute them on the database with UNION ALL, everything is fine. So it is some Entity Framework bug. Does anyone have an idea how to avoid it?

Upvotes: -1

Views: 75

Answers (1)

Stefan Dantchev
Stefan Dantchev

Reputation: 1

I reworked the object created in the Select statement of both queries by specifying separate properties rather than the Instance property and it worked. Still it is strange that EF cannot map Instance object once it is an object from the database.

Upvotes: 0

Related Questions