Saturday, May 10, 2008

Sample of LINQ

In the following sample we will search for the customer having greater than the specified amount and will show there name in the message box

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace SampleLinqApplication
{
public partial class CustomerDisplay : Form
{
public CustomerDisplay()
{
InitializeComponent();
}

private void findCustomer_Click(object sender, EventArgs e)
{
LinqSample sample = new LinqSample();
sample.FindCustomers();
}
}

class Customer
{
public string Name;
public string City;
public double Amount;
}

class LinqSample
{
public void FindCustomers()
{
Customer[] customers = {
new Customer { Name = "Anirudh", Amount = 9000, City = "Gurgaon" },
new Customer { Name = "Omkar Nath", Amount = 12202, City = "Nasik"},
new Customer { Name = "Peter", Amount = 123000, City = "Washington DC" },
new Customer { Name = "Madhav", Amount = 65553, City = "Bhopal"},
new Customer { Name = "Manpreet Singh", Amount = 789445, City = "Ludhiana"},
new Customer { Name = "Chin Yung", Amount = 5689, City = "Shanghai"}
};

var Query = from c in customers
where c.Amount > 10000
orderby c.Name
select c;

foreach (Customer cust in Query)
{
MessageBox.Show(cust.Name);
}
}
}
}

No comments: