Updated April 4, 2023
Introduction to LINQ Where in List
LINQ Where in List is a collection of elements with filter operation, here list items and where combined together to produce a result. It is a list of collected items with use of where condition. By using LINQ List we can filter the elements with the least coding with the help of where condition.
Syntax:
Let’s see the syntax of the Where in list clause, are of two methods used one is method syntax and another is query syntax, Let’s see the following syntax for clear understanding,
Method Syntax
List<Class_Name> _methodSyntax = ListItems.Where(user => user.userAge > 25). ToList< Class_Name >();
Query Syntax
List<Class_Name> querySyntax = (from user in ListItems
where user.userAge > 25
select user).ToList<Class_Name>();
How where in list works in LINQ?
The purpose is used to filter the data depends upon the condition basis with the collection of list elements. It belongs to the category of filtering operator. This LINQ Where in list is applicable in both the syntax method syntax uses the lambda expression whereas query syntax used only the expressions. Let’s see examples of LINQ where in list of elements as follows,
Let’s create the Course details class with its attributes,
class CourseDetails
{
public int courseID { get; set; }
public string subjectName { get; set; }
public int rankPlace { get; set; }
}
Create the collection of list with the CourseDetails class
List<CourseDetails> courseList = new List<CourseDetails>();
and add the basic details to the courseList like coursed, name of subject and place of rank holds as follows,
courseList.Add(new CourseDetails
{
courseID = 1,
subjectName = "LINQ Tutorials",
rankPlace = 5
});
courseList.Add(new CourseDetails
{
courseID = 2,
subjectName = ".NET Threading Tutorials",
rankPlace = 4
});
courseList.Add(new CourseDetails
{
courseID = 3,
subjectName = "Learn WPF",
rankPlace = 3
});
courseList.Add(new CourseDetails
{
courseID = 4,
subjectName = "ASP.NET Tutorials",
rankPlace = 5
});
courseList.Add(new CourseDetails
{
courseID = 5,
subjectName = "ADO.NET Leaners",
rankPlace = 2
});
LINQ Where is List in Query Syntax
In Lambda Expression we were retrieving the result of course name list based on the condition with the place of rank which is greater than 2.
Method Syntax
List<CourseDetails> result = (from course in courseList
where course.rankPlace > 2
select course).ToList<CourseDetails>();
In Query Expression, apply the same as above condition,
List<CourseDetails> result = courseList.Where(c => c.rankPlace > 2).ToList<CourseDetails>();
To displaying the result we using the foreach loop to get the result based on criteria as follows,
foreach (var course in result)
{
Console.WriteLine(course.subjectName);
}
Examples
LINQ Where in List is a collection of list items with the use of where condition. With using where condition we can filter the elements to limit the records with the help of where condition. This LINQ Where in list is applicable in both the syntax method syntax uses the lambda expression whereas query syntax used only the expressions.
Program #1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinqOrderByDesc
{
class Program_LINQ_ListInWhere
{
public class UserDetails
{
public int UserID
{
get;
set;
}
public string User_Name
{
get;
set;
}
public string User_Education
{
get;
set;
}
public int User_Salary { get; set; }
public int Dept_ID { get; set; }
public int User_Age { get; set; }
public static List<UserDetails> User_Records()
{
return new List<UserDetails>()
{
new UserDetails() {UserID = 1000, User_Name = "Shasha",Dept_ID=1,User_Education = "MCA", User_Age=21,User_Salary=75000},
new UserDetails() {UserID = 1005, User_Name = "Lalith", Dept_ID=2,User_Education = "B.E",User_Age=23, User_Salary=56000},
new UserDetails() {UserID = 1001, User_Name = "David",Dept_ID=1,User_Education = "MCA",User_Age=21, User_Salary=35000},
new UserDetails() {UserID = 1007, User_Name = "James",Dept_ID=3, User_Education = "B.E",User_Age=27,User_Salary=48000},
new UserDetails() {UserID = 1003, User_Name = "Rockey",Dept_ID=1, User_Education = "M.Sc",User_Age=21, User_Salary=52000},
new UserDetails() {UserID = 1004, User_Name = "Cheenu",Dept_ID=3, User_Education = "B.E",User_Age=27, User_Salary=89000},
};
}
}
static void Main(string[] args)
{
Console.WriteLine("\n\tLINQ - Where In List\n");
// retrieving the user details using where condition to get the user salary who getting lesser than 50000
List<UserDetails> Get_UserResult = UserDetails.User_Records().Where(e => e.User_Salary < 50000).ToList<UserDetails>();
Console.WriteLine("\tUser Salary Details whose getting lesser than 50000\n");
Console.WriteLine("\tUser Name\tUser Salary\n");
Console.WriteLine("\t---------\t-----------\n");
foreach (var userResult in Get_UserResult)
{
Console.WriteLine("\t"+userResult.User_Name+"\t\t"+userResult.User_Salary+"\n");
}
Console.WriteLine("\t---------\t-----------\n");
Console.ReadKey();
}
}
}
In the above coding we retrieving the user details by setting where condition to get the user salary who getting lesser than 50000, it is a method syntax to retrieve the records as follows
List<UserDetails> Get_UserResult = UserDetails.User_Records().Where(e => e.User_Salary < 50000).ToList<UserDetails>();
It retrieves the list of items based on the conditions applies, getting the records of users only who is getting salary less than 50000 with their names.
Output:
Program #2
In this program, it uses both syntaxes one is method syntax which uses the lambda expression whereas query syntax used only the expressions. Let’s see the following sample program.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Linq_WhereInList
{
class CourseDetails
{
public int courseID { get; set; }
public string subjectName { get; set; }
public int rankPlace { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<CourseDetails> courseList = new List<CourseDetails>();
courseList.Add(new CourseDetails
{
courseID = 1,
subjectName = "LINQ Tutorials",
rankPlace = 5
});
courseList.Add(new CourseDetails
{
courseID = 2,
subjectName = "DotNet Threading Tutorials",
rankPlace = 4
});
courseList.Add(new CourseDetails
{
courseID = 3,
subjectName = "Learn WPF",
rankPlace = 3
});
courseList.Add(new CourseDetails
{
courseID = 4,
subjectName = "ASP.NET Tutorials",
rankPlace = 5
});
courseList.Add(new CourseDetails
{
courseID = 5,
subjectName = "ADO.NET Leaners",
rankPlace = 2
});
Console.WriteLine("LINQ Where in List");
Console.WriteLine("------------------");
Console.WriteLine("\nQuery Syntax\n");
List<CourseDetails> querySyntax = (from course in courseList
where course.rankPlace > 2
select course).ToList<CourseDetails>();
foreach (var course in querySyntax)
{
Console.WriteLine(course.courseID+". "+course.subjectName);
}
Console.WriteLine("\n\nMethod Syntax\n");
List<CourseDetails> methodSyntax = courseList.Where(c => c.rankPlace > 2).ToList<CourseDetails>();
foreach (var course in methodSyntax)
{
Console.WriteLine(course.courseID+". "+ course.subjectName);
}
Console.ReadLine();
}
}
}
Output:
Conclusion
In this article, we learned the various examples programmatically for ease of understanding. Like this, we using the LINQ queries to retrieve the desired result. Hope the article helps to understand easily.
Recommended Articles
We hope that this EDUCBA information on “LINQ Where in List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.