这应该是一个简单的联合查询,但是即使操作类型相同,也会向我抛出错误:


  实例参数:无法从System.Linq.IQueryable<AnonymousType#1>转换为System.Linq.ParallelQuery<AnonymousType#2>


如果我删除此列,则工作正常。在第一个选择中,我想将操作值分配为“ 0”。任何帮助表示赞赏。谢谢

(from p in Pages join pa in PageAct on p.PageId equals pa.PageId
 select new
 {
     PageId = p.PageId,
     PgName = p.PgName,
     ActionId = pa.ActionId,
     ActionName = pa.ActionName,
     **action = 0**
 }).Union(from p in Pages
 join pa in PageAct on p.PageId equals pa.PageId
 join rp in RolePerm on pa.ActionId equals rp.ActionId into jrs
 from jrResult in jrs.DefaultIfEmpty()
 where jrResult.RoleId == 1
 select new
 {
     PageId = p.PageId,
     PgName = p.PgName,
     ActionId = pa.ActionId,
     ActionName = pa.ActionName,
     action = jrResult.ActionId
 })

最佳答案

匿名对象应该具有完全相同的签名,才能视为相同。

如果RolePerm.ActionId是可为空的类型,则第一个匿名对象属性类型也应为可为空。
实际类型也可以是不可为null的类型,但是由于这是左连接,因此您不应忘记将其显式转换为可为null的int,否则可能会遇到运行时异常。

(from p in Pages join pa in PageAct on p.PageId equals pa.PageId
 select new
 {
     PageId = p.PageId,
     PgName = p.PgName,
     ActionId = pa.ActionId,
     ActionName = pa.ActionName,
     action = (int?)0
 }).Union(from p in Pages
 join pa in PageAct on p.PageId equals pa.PageId
 join rp in RolePerm on pa.ActionId equals rp.ActionId into jrs
 from jrResult in jrs.DefaultIfEmpty()
 where jrResult.RoleId == 1
 select new
 {
     PageId = p.PageId,
     PgName = p.PgName,
     ActionId = pa.ActionId,
     ActionName = pa.ActionName,
     action = (int?)jrResult.ActionId // should cast, even if ActionId is not nullable
                                      // because left join might give null
 })

关于c# - EF联盟问题“无法从<AnonymusType#1转换为AnonymusType#2>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43797976/

10-11 08:07