How to Filter the data by ofType method using Linq concept

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 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:-


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)
    var query_str = element.OfType<string>();
    foreach (var st2 in query_str)
    var query_double = element.OfType<double>();
    foreach (var st3 in query_double)

  • 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:-


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

I hope this helpful for you.

Download Attached File


Post a Comment

Powered by Blogger.