Ehsan Tavakoli

Writing About .NET Programming and other stuff

Archive for March 2010

Simple code to use LINQ on a Generic list

with 2 comments

What is LINQ?

Most programmers today are required to integrate some sort of data into their applications. Often, you have to take data from multiple sources such as memory collections, relational databases, XML files, etc. With the current implementation of .NET Framework, getting to this data is often tedious and requires familiarity with multiple data access technologies and XML APIs. To make matters worse, all data sources have different means of querying the data in them: SQL for databases, XQuery for XML, LDAP queries for Active Directory etc. In short, today’s data access story lacks a unified approach to accessing data from disparate data sources, which is exactly what the LINQ (Language INtegrated Query) family of technologies are intended to solve.

Introduction to LINQ

The official goal of LINQ family of technologies is to add “general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data”. One of the nice things about LINQ is that it integrates seamlessly with the existing .NET languages such as C#, VB.NET because the underlying LINQ API is just nothing but a set of .NET classes that operate like any other .NET class. In addition, the query functionality is not just restricted to SQL or XML data; you can apply LINQ to query any class as long as that class implements IEnumerable class.

read it more here: http://www.15seconds.com/Issue/060713.htm

Here is a sample code to use LINQ on a generic list in ASP.NET 3.5. First, I’m going to create a sample class (Customer) with three variables: name, family and phone. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Customer
{
    private String _name;
    private String _family;
    private String _phone;

	public List<Customer> fillCustomer()
	{
            List<Customer> customerList = new List<Customer>();
            customerList.Add(new Customer("Ehsan","Tavakoli","0234234234"));
            customerList.Add(new Customer("John", "Smith", "0366544574"));
            customerList.Add(new Customer("Eddie", "Williams", "085448782"));

            return customerList;
	}

    public Customer() { }

    public Customer(String name, String family, String phone)
    {
        _name = name;
        _family = family;
        _phone = phone;
    }

    public String name
    {
        get {return _name;}

        set {_name = value;}
    }

    public String family
    {
        get {return _family;}

        set {_family = value;}
    }

    public String phone
    {
        get {return _phone;}

        set {_phone = value;}
    }
}

As you can see, there are two Constructors for this class: Customer() and Customer(String name, String Family, String Phone) and there is method which generate our generic list of Customer

public List<Customer> fillCustomer()
{
            List<Customer> customerList = new List<Customer>();
            customerList.Add(new Customer("Ehsan","Tavakoli","0234234234"));
            customerList.Add(new Customer("John", "Smith", "0366544574"));
            customerList.Add(new Customer("Eddie", "Williams", "085448782"));

            return customerList;
}

In this method, first we create a list of Customer(s) and then add 3 sample Customer into this list.

Now, let look at our page:

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
        Customer customer = new Customer();
        List<Customer> customerList = customer.fillCustomer();


        IEnumerable<Customer> customerQuery =
        from cust in customerList
        where cust.name == "Ehsan"
        select cust;
        
        foreach (Customer customerrow in customerQuery)
        {
            Response.Write(customerrow.family + ", " + customerrow.name);
        }

    }
}

you can see, we filled the “customerList” with our method. And then we use a Linq query over customerList. In this case, we just select customers with the first name “Ehsan”. And finally we just print them out using foreach loop.

Learn LINQ from MSDN:
http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

Advertisements

Written by Ehsan Tavakoli

March 8, 2010 at 12:16 pm

Posted in asp.net, Generic, Linq