日期:2014-05-17  浏览次数:20657 次

linq高手帮帮忙,关于linq多表左连接查询出错!在线等,急急急
如有A、B、C、D、E 五张表,他们的关系可以由以下的sql语句体现出来
sql中这几张表的左连接查询为:
select * from A 
left join B on A.bid=B.id
left join C on B.cid=C.id
left join D on C.did=D.id
left join E on D.eid=E.id
where 1=1

现在改为linq实现以上的sql语句,大侠们帮忙啊,我这样写报错:
C# code

var query = from a in data.A
            join b in data.B
            on a.bid equals b.id into ab
            from r1 in ab.DefaultIfEmpty()
            join c in data.C
            on r1.cid equals c.id into bc
            from r2 in bc.DefaultIfEmpty()
            join d in data.D
            on r2.did equals d.id into cd
            from r3 in cd.DefaultIfEmpty()
            join e in data.E
            on r3.eid equals e.id into ce
            from r4 in ce.DefaultIfEmpty()
            select new Signature
            {
                  Id = a.Id,
                  Name = r1.Name,
                  Pro = r2.Pro,
                  IpAdd = r3.IpAdd
            }; 
   IList<Signature> list = query.Take(pageSize).Skip((page - 1) * pageSize).ToList();//在这句报错
   return list;


具体的报错信息为:无法将类型为“Remotion.Linq.Clauses.JoinClause”的对象强制转换为类型“Remotion.linq.Clauses.FromClauseBase",引发了“System.InvalidCastException”类型的异常,
求大侠帮忙,急急急啊!!!

------解决方案--------------------
C# code

var query = from a in data.A
            join b in data.B
            on a.bid equals b.id into ab
            from b in ab.DefaultIfEmpty()
            join c in data.C
            on b.cid equals c.id into bc
            from c in bc.DefaultIfEmpty()
            join d in data.D
            on c.did equals d.id into cd
            from d in cd.DefaultIfEmpty()
            join e in data.E
            on d.eid equals e.id into ce
            from e in ce.DefaultIfEmpty()
            select new Signature
            {
                  Id = a.Id,
                  Name = b==null?"":b.Name,
                  Pro = c==null?"":c.Pro,
                  IpAdd = d==null?"":d.IpAdd
            };