connectseekers.com

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 7

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

7 comments:

  1. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training | rpa training in Chennai

    ReplyDelete
  2. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
    AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    Advanced Amazon Web Services Training in Pune | Best AWS Training in Pune
    AWS Online Training | Best Online AWS Certification Course - Gangboard
    Best Top 110 plus AWS Interview Question and Answers 2019

    ReplyDelete
  3. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    python course in pune
    python course in chennai
    python Training in Bangalore

    ReplyDelete
  4. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Best Devops Training in pune

    ReplyDelete
  5. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

    ReplyDelete

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

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