日期:2014-05-20  浏览次数:20806 次

请教关于linq左连接的问题
数据结构大概是这样:
C# code

class Category()
{
    public int Id {get;set;}
    public int ParentId {get;set;}
    public string Name {get;set;}
}



我想写出这样的效果:
SQL code

select c.*, p.Name as ParentName
from Category as c
left join Category as p
on c.ParentId = p.Id
where c.Id=1



然后这是我写的linq:
C# code

from c in Category.Where(c => c.Id == 1)
join p in Category
on c.ParentId equals p.Id into cates
from cate in cates.DefaultIfEmpty()
select new
{
    c,
    ParentName = p.Name
};



但是在select new里面无法访问p.Name,为什么呢?谢谢大家!

------解决方案--------------------
C# code
from c in Category.Where(c => c.Id == 1)
join p in Category
on c.ParentId equals p.Id into cates
from cate in cates.DefaultIfEmpty()
select new
{
    c,
    ParentName = cate.Name
};

------解决方案--------------------
探讨
数据结构大概是这样:
C# code

class Category()
{
public int Id {get;set;}
public int ParentId {get;set;}
public string Name {get;set;}
}



我想写出这样的效果:
SQL code

select c.*, p.Name as ParentNam……