基础SQL转LINQ问题
asp.net中我想写一个简单的not in子查询,用sql的意思是select * from tab1 where tab1.CourseID not in (select CourseID from tab1 where tab1.ClassType ==2 && tab1.CourStarTime < DateTime.Now)
我自己照着例子写一下,可是不行,麻烦各位指教一下,我初学还不太明白写法,下面是我写的代码,执行不通过。
public List<CourseClassState> GetCourseStateList()
{
var query1 = from p in db.tab1
select p;
var query2 = from q in db.tab1
where q.ClassType == 2 && q.CourStarTime < DateTime.Now
select q.CourseID;
return query1.Where(m => !query2.Contains(m.CourseID)).ToList();
}
------解决方案--------------------画蛇添足了
select * from tab1 where tab1.CourseID not in (select CourseID from tab1 where tab1.ClassType ==2 && tab1.CourStarTime < DateTime.Now)
就是
select * from tab1 where ClassType <> 2 or CourStarTime >= DateTime.Now
------解决方案--------------------
//Linq to Entity
var result = entity.tab1.Where(a => !(entity.tab1.Where(c => c.ClassType == 2 && c.CourStarTime < DateTime.Now).Select(c => c.CourseID)).Contains(a.CourseID));