读书人

关于linq to entity Left join 的有关

发布时间: 2012-12-14 10:33:08 作者: rapoo

关于linq to entity Left join 的问题
select * from [table1] a
left join [table2] b on a.ID = b.ID
left join [table3] c on a.ID = c.ID
left join [table4] c on a.ID = c.ID
.....
...

如何转换成LinQ啊...

貌似用了Linqer也没用

[最优解释]
from c in table1
join p in table2 on c.ID equals p.PID into t1
from p in t1.DefaultIfEmpty()
join d in table3 on p.EID equals pi.PIID into t2
from d in t2.DefalutIfEmpty()
select c;
[其他解释]


from c in table1
join p in table2 on c.ID equals p.PID
join d in table3 on p.EID equals pi.PIID
select new
{
c.ID, p.PID, pi.PIID
}


[其他解释]
引用:
C# code123456789from c in table1join p in table2 on c.ID equals p.PID join d in table3 on p.EID equals pi.PIID select new{ c.ID, p.PID, pi.PIID}


0. 0其实看得不是很懂..
[Table1]中的ID包含了其他所有表中的ID并且其他表中的ID是不相同的
这种情况下光是用join进行多表联合查不到数据...
[其他解释]
引用:
from c in table1
join p in table2 on c.ID equals p.PID into t1
from p in t1.DefaultIfEmpty()
join d in table3 on p.EID equals pi.PIID into t2
from d in t2.DefalutIfEmpty()
……

..我用这种方法结果是
base {System.SystemException} = {"到值类型“Guid”的强制转换失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可以为 null 的类型。"}

我的LINQ是这样写的

var result = (from a in table1
join b in table2 on a.a_guid equals b.b_guid into t1
from b in t1.DefaultIfEmpty()
join c in table3 on a.a_guid equals c.c_guid into t2
from c in t2.DefaultIfEmpty()
join e in table4 on a.a_userid equals e.e_userid into t3
from e in t3.DefaultIfEmpty()
orderby a.CreateWhen
select new {....});


------其他解决方案--------------------


=. =好吧我知道了 因为 当table2 有值的时候 table3 必然是空的..反之同理..
当遇到这种情况的时候应该肿么解决呢
[其他解释]
帖出你的select new里的代码
[其他解释]
0. 0 貌似我是解决了


select new
{
a,
b,
c,
e
//DynamicGUID = a.DynamicGUID,
//DynamicType = a.DynamicType,
//CreateWhen = a.CreateWhen,
//UserID = e.UserID,
//UserGUID = e.UserGUID,
//Avatar = e.Avatar,
//Name = e.Name,
//Body = b.Body,
//SimpleCategory = c.SimpleCategory,
//IndustryCategory = c.IndustryCategory,
//AreaCategory = c.AreaCategory,


//JobCategory = c.JobCategory,
//DepartmentCategory = c.DepartmentCategory,
//BusinessDescription = c.BusinessDescription
});


之前select的是注释了的东西..现在直接select a b c e..然后foreach 用 item.a/b/c/e ...
这样=. = 好像可以了
[其他解释]
你要理解原因
比如这句:
UserID = e.UserID,
因为e有可能为NULL,所以要这么写:
UserID =e==null?0:e.UserID,
才不会报错
[其他解释]
引用:
你要理解原因
比如这句:
UserID = e.UserID,
因为e有可能为NULL,所以要这么写:
UserID =e==null?0:e.UserID,
才不会报错

0. 0嗯....谢谢指教~~

读书人网 >.NET

热点推荐