关于条件和列连接的SQL到Linq

时间:2016-12-13 17:25:29

标签: sql linq

这听起来很简单,但我想将下面的sql转换为linq。

  Select C.CompanyId, C.CompanyName, CompanyNumber, IsNull(UC.IsActive, 0) as IsActive
  From Company as C
  Left Join UserCompany UC on UC.CompanyId = C.CompanyId and UC.UserId = '2E34B10C-1BDB-4EF9-ADDF-4C5F949C19B1'  
  Order by C.CompanyNumber

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样?显然,您需要添加实体上下文与我的列表。

        var results = (
            from c in new List<Company>()
            join uc in (
                from x in new List<UserCompany>()
                where x.UserId == "2E34B10C-1BDB-4EF9-ADDF-4C5F949C19B1"
                select x
            ) on c.CompanyId equals uc.CompanyId into gj
            from uc in gj.DefaultIfEmpty()
            select new
            {
                c.CompanyId,
                c.CompanyName,
                c.CompanyNumber,
                uc.IsActive
            }
        ).ToList();