Using “where” clause in linq is just like writing where clause in SQL query, helps filtering records from actual result-set.
We can use filtering operator "where" in LINQ to filter the sequence of collection object based on any given criteria.
The LINQ Where operator is actually an extension method, filters the collection based on any given criteria expression, can return a single object or a new collection
The criteria can be specified as Func delegate type or lambda expression.
Example of using Where Clause with Ado.Net DataTable
DataSet _ds = null; using (StudentDTO dto = new StudentDTO()) { _ds = dto.getStudentDataset(); } var _students = from dt in _ds.Tables[0].AsEnumerable() where (dt.Field<string>("FullName").StartsWith("A")); select new { Name = dt.Field<string>("FullName"), Address = dt.Field<string>("Address") };
Another example of Where Clause with Entity
List<tbStudent> _students = null; using (Entities context = new Entities()) { _students = context.tbStudents .Where(s=>s.FullName.Contains("wa")) .ToList<tbStudent>(); }
Another way to write, we can have where clause with multiple conditions.
using (Entities context = new Entities()) { var student = (from s in context.tbStudents where s.FullName == "Anu" select s).FirstOrDefault<tbStudent>(); }
As you can see in above examples, how differently where clause can be used, like with StartsWith, Contains, name equal etc. Similarly you can also use where clause with EndsWith, not equal etc.