Saturday 6 July 2013

Using Type Filtering and Where Clause in Linq


Type Filtering in Linq: you can  use the where clause to filter your data on basis of  a particular type.For filtering base of  type,you can use the  OfType() extension method…
Where Clause  in Linq:  The where clause is the filtering operator in Linq which is  used to define the  restrictions on the basis of which element returned

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TypeFilteringInLinq
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string result;

//code  for  Type Filtering ................

private void btnTypeFiltering_Click(object sender, EventArgs e)
{
object[] days = { 1, "Sunday", 2, "Monday", 3, "Tuesday", 4, "Wednesday", 5, "Thursday", 6, "Friday", 7, "Saturday" };

if (cbfiltertype.Text == "Int" )
{
result = "";
var query = days.OfType<int>();
foreach (var d in query)
{
result += d.ToString() + ",";
}
lblmessage.Text = result.Remove(result.Length - 1);
}
if (cbfiltertype.Text == "String")
{
result = String.Empty;
var query = days.OfType<string>();
foreach (var d in query)
{
result += d.ToString() + ",";
}
lblmessage.Text = result.Remove(result.Length - 1);
}

}
//code  for  Where Clause................
private void btnWhereClause_Click(object sender, EventArgs e)
{
result = String.Empty;
int[] no = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40 };
var myno = from n in no where n > 20 select n;
foreach (var x in myno)
{
result += x.ToString() + ",";
}
lblmessage.Text = result.Remove(result.Length - 1);
}

}
}

0 comments:

Post a Comment