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 }));

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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With