c# - How do I resolve an error on Union within IQueryable Select statement -


i wrote iqueryable select statement , tried declare union, reason union because going include code later magic , found union appropriate method i'll not it's nothing error.

the error returned visual studio is

error   6    cannot implicitly convert type 'system.linq.iqueryable<anonymoustype#1>'  'system.linq.iqueryable<myproject.models.tbl_customers>'.  explicit conversion exists (are missing cast?) 

the select statement is:

var datacontext = db.tbl_customers.asqueryable();  iqueryable<customerviewmodel> thecustomer = (                p in datacontext                select new {                    customer_id = p.vessel_idx,                    customer_name = p.customer_name                                                                         })                .union ((                     p in datacontext                    select new {                        customer_id = p.vessel_idx,                        customer_name = p.customer_name                                                                           })); 

i tried adding .tolist didn't help. can suggest way fix this?

agreed @har07, both queries project result using same anonymous type (they share same property names , types). problem thecustomer variable iqueryable<customerviewmodel>. way of solve problem projecting queries viewmodel class:

iqueryable<customerviewmodel> thecustomer = (                p in datacontext                select new customerviewmodel{                    customer_id = p.vessel_idx,                    customer_name = p.customer_name                                                                         })                .union ((                     p in datacontext                    select new customerviewmodel{                        customer_id = p.vessel_idx,                        customer_name = p.customer_name                                                                           })); 

in case i'm assuming view model class has properties same names of anonymous type, that's can change @ convenience.


Comments