Sandesh Kirani
Sandesh Kirani

Reputation: 59

Does Entity Framework Core support Union?

I am trying to query multiple tables in EF Core using union as below, but it does not allow. Please find the query and also attached the image on the error hint that .net core frame work provides.

 var query =
                _context.Brand.Select(x => new { BrandID = x.Brandid })
                    .Union(_context.Factory.Select(x => new { Fa = x.Factorycode }))
                    .Union(_context.Brandfactory.Select(x => new { BrFc = x.Factoryid }));

enter image description here

Upvotes: 6

Views: 27317

Answers (1)

M. Galczynski
M. Galczynski

Reputation: 634

You try to have Union of different anonymous types, please use same types or don't use anonymous types at all, like in my snippet

var query =
                _context.Brand.Select(x => x.Brandid)
                    .Union(_context.Factory.Select(x => x.Factorycode))
                    .Union(_context.Brandfactory.Select(x => x.Factoryid));

Please also remember that EF core don't evaluate yet Union on database site, it will be evaluated locally. More details in this issue

Upvotes: 3

Related Questions