Does Entity Framework Core support Union?

entity-framework-core linq

Question

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

1
2
2/13/2019 6:15:36 AM

Popular Answer

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

2
1/16/2020 4:14:03 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow