In this
article I will show you how you can apply where clause in datatable with and,
or clause using c#.net and VB.net.Datatable Where Clause or Condation With AND, OR Operation in Linq C#.Net | Datatable.select with Where Condition In C#.
So for this article first we will create a new console application and in this application we will add the below function which will hold some data for performing operation.
So for this article first we will create a new console application and in this application we will add the below function which will hold some data for performing operation.
private static DataTable FillStudentData()
{
DataTable dtstudent = new DataTable();
//Adding columns to table Emp
DataColumn
colEmpid = new DataColumn("RollNo", typeof(System.Int32));
DataColumn
colName = new DataColumn("Name", typeof(System.String));
DataColumn
colDept = new DataColumn("Address", typeof(System.String));
//Adding columns to datatable
dtstudent.Columns.AddRange(new DataColumn[] { colEmpid, colName, colDept });
//Adding data
dtstudent.Rows.Add(1, "John Smith
Brown", "Address 1");
dtstudent.Rows.Add(2, "Carry
Brown", "Address 2");
dtstudent.Rows.Add(3, "Candle
Pencil", "Address 3");
dtstudent.Rows.Add(4, "Graham
Bell", "Address 4");
dtstudent.Rows.Add(5, "Peter
Kevin", "Address 5");
return dtstudent;
}
|
After this
here is the code for where clause with and operation.
AND Operation:
C#.Net:
DataTable dtstudent = new DataTable();
dtstudent = FillStudentData();
DataRow[] filterData1 =
dtstudent.Select("RollNo=5 and Name='Peter Kevin'");
foreach (DataRow item in filterData1)
{
Console.WriteLine("Roll No : "
+ item.Field<int>("RollNo") + ", Name :
" + item.Field<string>("Name") + ", Address : "
+ item.Field<string>("Address"));
}
Console.ReadLine();
|
VB.Net:
Dim dtstudent As New DataTable()
dtstudent = FillStudentData()
Dim filterData1 As DataRow() =
dtstudent.[Select]("RollNo=5 and Name='Peter Kevin'")
For Each item As DataRow In filterData1
Console.WriteLine("Roll
No : " + item.Field(Of Integer)("RollNo") + ", Name : " +
item.Field(Of String)("Name") + ", Address : " +
item.Field(Of String)("Address"))
Next
Console.ReadLine()
|
Now check
the output
OR Operation:
C#.Net:
DataTable dtstudent = new DataTable();
dtstudent = FillStudentData();
DataRow[] filterData1 =
dtstudent.Select("RollNo=3 or Name='Peter Kevin'");
foreach (DataRow item in filterData1)
{
Console.WriteLine("Roll No : "
+ item.Field<int>("RollNo") + ", Name :
" + item.Field<string>("Name") + ", Address : "
+ item.Field<string>("Address"));
}
Console.ReadLine();
|
VB.Net:
Dim dtstudent As New DataTable()
dtstudent = FillStudentData()
Dim filterData1 As DataRow() =
dtstudent.[Select]("RollNo=3 or Name='Peter Kevin'")
For Each item As DataRow In filterData1
Console.WriteLine("Roll
No : " + item.Field(Of Integer)("RollNo") + ", Name : " +
item.Field(Of String)("Name") + ", Address : " +
item.Field(Of String)("Address"))
Next
Console.ReadLine()
|
Now check
the output.
0 comments:
Please let me know your view