Friday, 16 August 2013

How ienumerable and iqueryable working

How ienumerable and iqueryable working

Hi im having the following code and im just working on IEnumerable and
IQueryable
List<Customer> c = new List<Customer>()
{
new Customer(){ CustomerID=1, Name="x", Orders=new List<Order>()
{
new Order(){ CustomerID=1,OrderID=1},
}
},
new Customer(){ CustomerID=1, Name="x", Orders=new List<Order>()
{
new Order(){ CustomerID=1,OrderID=2},
}
},
};
// if i use this code im getting x , x as output
IQueryable<Customer> gg1 = c.AsQueryable().Where(d =>
d.CustomerID == 1);
// if i use this code im getting x , x as output
IEnumerable<Customer> gg = c.AsEnumerable().Where(d =>
d.CustomerID == 1);
foreach (Customer dc in gg)
{
Console.WriteLine(dc.Name);
}
Console.ReadLine();
for both IEnumerable and IQueryable im getting the same answer ,
my question is
1. why we need IQueryable
2. is my code is correct ?
3. When to use Ienumerable and Iqueriable?
4. what is the advantage of having this ?

No comments:

Post a Comment