Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Thursday, April 19, 2012

Filter a List <T> using LINQ


LINQ can be used effectively to filter out a set of objects from a List of objects, in todays post we shall see on how to accomplish object filtering using LINQ.

To know more about binding a List object to a DropDownList refer to the post, Binding a List <T> object to a DropDownList control


First let us create a class whoose objects will be stored in the List <T> here is the code for the class.

public class clsCountry
{
    public string _CountryCode;
    public string _CountryName;
    //
    public clsCountry(string strCode, string strName)
    {
        this._CountryCode = strCode;
        this._CountryName = strName;
    }
    //
    public string CountryCode
    {
        get {return _CountryCode;}
        set {_CountryCode = value;}
    }
    //
    public string CountryName
    {
        get { return _CountryName; }
        set { _CountryName = value; }
    }
}

Next, let us create a list of objects based on our class clsCountry and store them in a List <T> object. Here is the code for the List <T>

List<clsCountry> lstCountry = new List<clsCountry>();
lstCountry.Add(new clsCountry("USA", "United States"));
lstCountry.Add(new clsCountry("UK", "United Kingdom"));
lstCountry.Add(new clsCountry("IND", "India"));

Next, we shall bind the List <T> object lstCountry to a DropDownList control. Here the code to bind the data.

drpCountry.DataSource = lstCountry;
drpCountry.DataValueField = "CountryCode";
drpCountry.DataTextField = "CountryName";
drpCountry.DataBind();

Notice that the DataValueField and DataTextField property of the DropDownList control are mapped to the Properties of the class CountryCode and CountryName, hence make sure to create properties for every member of the class so that they can be used while binding the data to controls.

Finally, we shall use LINQ to filter data from the lstCountry object and bind the filtered list to the dropdown control drpCountry. Here is the code to filter the List <T> using LINQ

var filteredCountries = from c in lstCountry
                        where c.CountryName.StartsWith("U")
                        select c;

drpCountry.DataSource = filteredCountries;
drpCountry.DataValueField = "CountryCode";
drpCountry.DataTextField = "CountryName";
drpCountry.DataBind();

Now the dropdown control will have only 2 items

United States
United Kingdom



Here we are using c.CountryName.StartsWith("U") which works like

CountryName LIKE 'U%' 

We can use c.CountryName.Contains("U"), method to make the filter work like

CountryName LIKE '%U%'

i.e, it will filter out all objects which have the character U anywhere in the CountryName property of the object. 


That’s it we have filtered a List <T> and bound the contents of the filtered List <T> to a DropDownList control.

Binding a C# List to a DropDownList control


Binding a C# List to a DropDownList control

In general we use a DataTable or a DataReader to bind data to a DropDownList control, today we shall see on how to bind an object of type List to a DropDownList control.

To know more about the List object refer to the post C# List  

First let us create a class whoose objects will be stored in the List, here is the code for the class.

public class clsCountry
{
    public string _CountryCode;
    public string _CountryName;
    //
    public clsCountry(string strCode, string strName)
    {
        this._CountryCode = strCode;
        this._CountryName = strName;
    }
    //
    public string CountryCode
    {
        get {return _CountryCode;}
        set {_CountryCode = value;}
    }
    //
    public string CountryName
    {
        get { return _CountryName; }
        set { _CountryName = value; }
    }
}

Next, let us create a list of objects based on our class clsCountry and store them in a List object. Here is the code for the List

List<clsCountry> lstCountry = new List<clsCountry>();
lstCountry.Add(new clsCountry("USA", "United States"));
lstCountry.Add(new clsCountry("UK", "United Kingdon"));
lstCountry.Add(new clsCountry("IND", "India"));

Finally we shall bind the List object lstCountry to a DropDownList control. Here the code to bind the data.

drpCountry.DataSource = lstCountry;
drpCountry.DataValueField = "CountryCode";
drpCountry.DataTextField = "CountryName";
drpCountry.DataBind();

Notice that the DataValueField and DataTextField property of the DropDownList control are mapped to the Properties of the class CountryCode and CountryName, hence make sure to create properties for every member of the class so that they can be used while binding the data to controls.

That’s it we have bound the contents of a List object to a DropDownList control.

Wednesday, April 18, 2012

C# List


C# List

The List class in C# can be used to store typed objects. It is similar to an array but can store objects of custom type.

Arrays can hold objects of the predefined type like Integer, String, Boolean etc, but what if we need to store a list of objects belonging to a custom class which we define, this is where the List class comes into play, we can use the List to store custom objects of any type. Another advantage of the List over arrays is that we need not specify the dimension of the List while declaring, the List can dynamically grow/shrink as and when we add/remove items from the list.
The List can hold both the predefined types like Integer, String, Boolean, etc and custom types defined by us.

The List class comes under the System.Collections.Generic namespace, so make sure to include this namespace whenever you are using List.

using System.Collections.Generic;

Syntax
List<Type> lstObjects = new List<Type>();  

Example

First let us create a class clsCountry, later we shall store objects of this class in the List, here is our custom class.

public class clsCountry
{
    public string _CountryCode;
    public string _CountryName;
    //
    public clsCountry(string strCode, string strName)
    {
        this._CountryCode = strCode;
        this._CountryName = strName;
    }
    //
    public string CountryCode
    {
        get {return _CountryCode;}
        set {_CountryCode = value;}
    }
    //
    public string CountryName
    {
        get { return _CountryName; }
        set { _CountryName = value; }
    }
}

Now let us create a List to store a collection of objects of type clsCountry.
Here is the code.

List<clsCountry> lstCountry = new List<clsCountry>();
lstCountry.Add(new clsCountry("USA", "United States"));
lstCountry.Add(new clsCountry("UK", "United Kingdon"));
lstCountry.Add(new clsCountry("IND", "India"));
//
int nCount = lstCountry.Count();

Now objects of type clsCountry are added to the list lstCountry, the variable nCount will have a value of 3, since we added 3 objects to the List.

That’s it we have seen the advantages of using List with an Example.