How to Filter the data by ofType method using Linq concept

By
Hi Friends ! Today i will show you "How to filter your mix data by ofType () method". You can use the Where clause to filter your data based on a particular type.Which i have explained in our previous tutorial here. You filer your data using ofType() method with Where clause.This is very simple code but we have to understand this concepts so that we can use this concepts on your small or large application based on requirements. There are following steps to implement this concepts on your asp.net application.
Step 1 :- First open your visual studio -->File -->New-->website -->Select AP.NET Empty webste -->OK -->Open Solution Explorer -->Add a new Web form(default.aspx) -->Drag and Drop 3 List box and a Button controls on the form as shown below:-



form

Step 2 :- Now Write the C# codes on the Default.apx.cs file(submit button click) as given below:-
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
protected void Button1_Click(object sender, EventArgs e)
{
    object[] element = { 1, 'r', "Delhi", 1.2,1.0, "Lucknow", 5, 4.56, 'a', 5.999999, "Bangalore", 9, 10.5, 'm', "Tokiyo", 6.004563, 's', 2.4, 34.9098763, "London", 6.00, 98, 5.00033393 };
    var query_int = element.OfType<int>();
    var num = from n in query_int
              where n > 4
              select n;
    foreach (var st1 in num)
    {
        ListBox1.Items.Add(st1.ToString());
    }
    var query_str = element.OfType<string>();
    foreach (var st2 in query_str)
    {
        ListBox2.Items.Add(st2.ToString());
    }
    var query_double = element.OfType<double>();
    foreach (var st3 in query_double)
    {
        ListBox3.Items.Add(st3.ToString());
    }
}
}


Note:-
  • you can use where clause in other parts of the codes also based on your requirements. 
Step 3 :- Now Run the Application (press F5)-->Press submit button -->you will see the following output as shown below:-


output

Note :- You can filter your database data also by using this concept .

I hope this helpful for you.

Download Attached File
     Download      

0 comments:

Post a comment

Powered by Blogger.