不知怎的,我无法弄清楚如何在语句的ON部分中转换linq参数的类型。该陈述将导致以下错误:
错误1 join子句中某个表达式的类型不正确。调用'GroupJoin'时类型推断失败。
ProjectID = pID的类型是int,以及r.ProjectID。那么有没有人知道导致这个错误的原因是什么?
ViewData.Model = (from c in _db.Category
join r in _db.CategoryAndProject2
on new { c.CategoryID, ProjectID = pID } equals new { r.CategoryID, r.ProjectID }
into join1
from j in join1.DefaultIfEmpty()
select new CategoryDTO
{
CatID = (int) j.CategoryID
}).Distinct().ToList();
编辑在线,因为缺少代码
答案 0 :(得分:0)
试试这个:
on (int?)c.CategoryID equals r.CategoryID
联接中不需要匿名类型。
答案 1 :(得分:0)
你可以这样做:
from c in _db.Category
join r in _db.CategoryAndProject2
on new { c.CategoryID, ProjectID = pID } equals
new { CategoryID = r.CategoryID ?? 0, r.ProjectID }
...
执行
可能更有效率from c in _db.Category
join r in _db.CategoryAndProject2
on new { CategoryID = (int?)c.CategoryID, ProjectID = pID } equals
new { r.CategoryID, r.ProjectID }
...
因为这会转换为字段上的直接连接而不进行转换。第一个查询包含COALESCE
,它排除了索引的使用。