Saturday, 2 June 2012

What is the difference between IEnumerable and IQuerable interfaces.


Yes,we have choose them as per our operation performed object such as Object is in-memory choose IEnumerable<T> interface. Object is database object such datasource.
IEnumerable<T>:  Exposes the enumerator, which supports a simple iteration over a collection a specified type.

note : inmemory objects means-àLinq to objects.

            int[] intarr = { 1, 2, 4, 5, 6 };
            IEnumerable<int> i = intarr.OfType<int>();

            IEnumerator<int> s = i.GetEnumerator();
            while (s.MoveNext())
            {
                int l = (int)s.Current;
                Console.WriteLine(l);
            }

IQuerable<T> : Provides functionality to evaluate queries against a specific data source wherein the type of the data is known.Here T is the type of the data in the data source.

note : datasource objects means->Linq to Sql objects(ORM).


Demo:

WcfdbDataContext dbobj=new WcfdbDataContext();

var/IQuerable<emp>*/IEnumerable<emp> empdata = from n in dbobj.emps select n;
        GridView1.DataSource = empdata;
        GridView1.DataBind();

No comments:

Post a Comment