"Where" is a query expression , "where" is used to specify an element or a
range of element with a given conditions. Where condition check each
element against the given condition, if checking return true, the
elements comes as an output. Multiple "where" condition can be apply in
LINQ query. Where condition can be apply anywhere in the query line ,except
first and last line of the query.
1)Below is the exmaple of simple "where" condition with a range of value.
Example
using System;
1)Below is the exmaple of simple "where" condition with a range of value.
Example
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
where
s.age > 10 && s.age < 14
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Output :
Name : John7
Roll : 127
Age : 11
Name : John8
Roll : 128
Age : 12
Name : John9
Roll : 129
Age : 13
Roll : 127
Age : 11
Name : John8
Roll : 128
Age : 12
Name : John9
Roll : 129
Age : 13
2)Below is the exmaple of simple "where" condition with string compare .The filter may be part of the string , starting of the string ,ending of the string etc.For part of the string "Contains" or "
Substring
" is used.For starting of the string ,"StartsWith" is used.For ending of the string "EndsWith" is used.Below example filter name that contain string "n6" in any position of the name field.
Example
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
where
s.name.Contains("n6")
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Name : John6
Roll : 126
Age : 10
3)LINQ support multiple "Where" keyword.Below example show how more than one "Where" keyword can be applied in the query.
Example
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 12 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 13 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 14 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 15 });
var
query = from
student
s in
myList
where
s.roll > 124
where
s.roll < 127
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Name : John5
Roll : 125
Age : 9
Name : John6
Roll : 126
Age : 10
4)LINQ support conditional if ,iif in the where condition.Below example show how iif can be added in the where condition.
Example
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 17 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 18 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 19 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 20 });
var
query = from
student
s in
myList
where
(s.age
== 18 ? s.age : 30) <= (s.age == 15 ? 1 : s.age)
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Roll : 129
Age : 18
4)LINQ support method in where condition.Below example shows how method can be used in When condition.
Example
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
ArrayList
myList = new
ArrayList();
myList.Add
(new
student
{name = "John1",
roll = 121,age = 5});
myList.Add(new
student
{ name = "John2",
roll = 122, age = 6 });
myList.Add(new
student
{ name = "John3",
roll = 123, age = 7 });
myList.Add(new
student
{ name = "John4",
roll = 124, age = 8 });
myList.Add(new
student
{ name = "John5",
roll = 125, age = 9 });
myList.Add(new
student
{ name = "John6",
roll = 126, age = 10 });
myList.Add(new
student
{ name = "John7",
roll = 127, age = 11 });
myList.Add(new
student
{ name = "John8",
roll = 128, age = 17 });
myList.Add(new
student
{ name = "John9",
roll = 129, age = 18 });
myList.Add(new
student
{ name = "John10",
roll = 1210, age = 19 });
myList.Add(new
student
{ name = "John11",
roll = 1211, age = 20 });
var
query = from
student
s in
myList
where
IsAdult(s)
select
s;
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "
+ c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
Console.ReadLine();
}
static
bool
IsAdult(student
s)
{
return
s.age >= 18 && s.age <= 25;
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Name : John9
Roll : 129
Age : 18
Name : John10
Roll : 1210
Age : 19
Name : John11
Roll : 1211
Age : 20