请教关于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 ParentNamefrom Category as cleft join Category as pon c.ParentId = p.Idwhere c.Id=1
然后这是我写的linq:
- C# code
from c in Category.Where(c => c.Id == 1)join p in Categoryon c.ParentId equals p.Id into catesfrom 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 Categoryon c.ParentId equals p.Id into catesfrom cate in cates.DefaultIfEmpty()select new{ c, ParentName = cate.Name};
[解决办法]