Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 479
Sandesh Kirani Avatar asked Aug 11 '26 19:08

Sandesh Kirani


1 Answers

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

like image 168
M. Galczynski Avatar answered Aug 14 '26 09:08

M. Galczynski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!