使用LINQ将两个不同类型的IQueryable变量连接在一起

sta*_*101 5 c# linq entity-framework

列名称和类型是相同的,但它来自两个独立的实体.这是一个简化的例子:

--Query View
var v_res = from s in db.V_CMUCUSTOMER
           select s;

--Values from table less values from view
var res = from s in db.CMUCUSTOMERs
          where !(from v in v_res
            select v.ID).Contains(s.ID)
         select s;

--join table and view values into one variable
var res_v_res = (from c in res
            select c).Union(from v in v_res
            select v);
Run Code Online (Sandbox Code Playgroud)

但是我得到以下错误:

实例参数:无法从"System.Linq.IQueryable"转换为System.Linq.ParallelQuery

Tim*_*ton 0

如果您指定一个新的匿名类型并为两者使用 ToList() 那么您应该能够按如下方式联合它们:

var v_res = (from s in db.V_CMUCUSTOMER
            select new { custName = s.customer_name custAddress = s.address}).ToList();

--Values from table less values from view
var res = (from s in db.CMUCUSTOMERs
          where !(from v in v_res
            select v.ID).Contains(s.ID)
          select new { custName = s.customer_name custAddress = s.address }).ToList();

--join table and view values into one variable
var res_v_res = v_res.Union(res);
Run Code Online (Sandbox Code Playgroud)

如果有几十个列,这可能会很繁重,但仍然可以工作。