如何在Linq中使用IN cluse

时间:2016-10-08 06:24:49

标签: asp.net-mvc

我有一个Sql查询如下,请帮我如何在Linq中编写确切的查询。感谢

select count(ExpiredProjectID) from AssignedExpiredProjects where ExpiredProjectID IN (select id from ExpiredProjectsTracking)

2 个答案:

答案 0 :(得分:1)

试试这段代码......

var projIDs= (from ept in _context.ExpiredProjectsTracking
              select ept.id).ToList();

int resultCount= (from apt in _context.AssignedExpiredProjects
                  where projIDs.Contains(apt.ExpiredProjectID)
                  select apt.ExpiredProjectID).Count();

答案 1 :(得分:0)

var searchList = context.ExpiredProjectsTracking.ToList();

var sum = context.AssignedExpiredProjects.Count(x => searchList.Contains(x.Id))