Friday, 17 November 2017

Datatable Where Clause or Condation With AND, OR Operation in Linq C#.Net | Datatable.select with Where Condition In C#

11/17/2017 - By Pranav Singh 0

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.

  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.


Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top