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

时间:2012-06-25 13:18:05

标签: 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);

但我收到以下错误:

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

2 个答案:

答案 0 :(得分:0)

当我在LINQPad中运行类似的查询时,收到一条错误消息,声称s中的Contains(s.ID)未在此上下文中定义。

如果我用&&替换where,则所有查询都会成功执行。

答案 1 :(得分: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);

如果有几十列但仍然有用,这可能会很麻烦。