In our SQL Query there is a situation when we want to fetch records based on records within the a Set of RecordID(primary key)
Lets look in to one example
I have a table
Employee
which has the columns EmpId, EmpName, EmpDesg etc.
You want to select the Employee Records from Employee table with EmpID within 61,62,63
So how we will write our normal sql query
Select * from [Employee] where EmpId in (61,62,63)
If we know what are the employee id’s we could hard code it.
But if the Query is like
Select * from [Employee] where id in (select empid from EmpAttendance where isAbsent=1)
In this case how we will write in LinQ,
I could get all EmpId from EmpAttendance in a List<int> array. then apply select in like query on Employee table.
This linQ C# code does that for you.
IEnumerable<int> list = new List<int>() { 61,6 2,6 3 };
List<Employee> query =
(from e in db.Employees
where list.Contains((int)e.EmpId)
select e).ToList();
Note: LINQ 2 SQL didn’t implement IN functionality in IList interface; instead, it’s implemented using IEnumerable interface. As a result, you have to “cast” e.Id to (int).
just some sample code. that’s all ok na.. hope it will be useful for somebody..
Discover more from Cloud Distilled ~ Nithin Mohan
Subscribe to get the latest posts sent to your email.