Suppose we have a user table with 10 records and you want to get the record only 8 records using the USERID.
Then we use SQL query as below.
- select * from user where userid not in(1,2)
This query display the record which id doesn't contain the 1 and 2.
But what about when we want to do same thing in EF using lambda expression.
Solution is below.
- List<long> lstUserIds = dataContext.Users.Where(c => c.UserId == 1 && c.UserId == 2 ).Select(c=>c.User1.UserID).ToList();
Then
- List<DAL.User> dalUserList = dataContext.Users.Where(c => !lstUserIds.Contains(c.UserID)).ToList();
or
- List<DAL.User> dalUserList = dataContext.Users.Where(c => lstUserIds.Contains(c.UserID)==false).ToList();
In first line we get the userid 1 and two from user table. And in second and third line we get the record which userid doesn't contain 1 and 2.