如何在Entity Framework中使用子查询?

时间:2016-10-30 13:11:39

标签: c# entity-framework linq

我有点卡在这上面。基本上我想做类似以下SQL查询的事情:

select 
    c.Code, c.name, c.Family,
    Parent= (select cc.name + '' + cc.Family 
              from Customers cc
              where cc.ID = c.F_ParentID) 
from 
    Customers c

1 个答案:

答案 0 :(得分:1)

我可以使用此代码而不是代码:

var q = from u in db.Customers
                let z = db.Customers
                       .Where(y => y.ID == u.F_ParentID)
                       .Select(y => y.Name + " " + y.Family).FirstOrDefault()
                select new
                {
                    u.ID,
                    Name = u.Name,
                    Family = u.Family,
                    Parent = z
                };