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

标签 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

最佳答案

如果您指定一个新的匿名类型并为两者使用 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);

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

关于c# - 使用 LINQ 将两个不同类型的 IQueryable 变量连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11190152/

相关文章:

c# - 将“/”更改为“\” [C#]

c# - 同时查找和映射

c# - 公开包含方法

entity-framework - ASP .Net - Entity Framework - MobileAppService - 将实体对象推送到后端时出错

java - 垃圾回收仅用于回收堆对象还是同时回收堆和堆栈中的对象?

c# - 如何仅使用 LocalDB 2016 发布我的 C# winform 程序?

sql - Linq 版 SQL "IN"语句

c# - Entity Framework 将实体映射到多个表?

c# - 类型 `Kaitai.Elf' 已经包含 `Bits' 的定义

c# - 从 List<T> 中删除重复值