Monday, 22 October 2012

How to use Projection Operatiors in LINQ?


we have many projection operators in linq like: Select(),where()....are Extension methods takes delegates as

input parameters


    var numbers = new[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
var values= numbers.Where((n, index) => (index % 2) != 0).ToList();
foreach(int item in values)
{
      Console.WriteLine(item.Tostring())
}
Here: numbers.Where(Fun<int,bool>,predicate)
based on source(numbers).so delegate points to integers.
another example is
  var names = new[] { "app", "balu", "charan", "dhanush" };

IEnumerable<string> stringvalues = names.Where(s => s.Contains("charan")).ToList();
            foreach (string item in stringvalues)
            {
                Console.WriteLine(item.ToString());
            }
Here: names.Where(Fun<string,bool>,predicate);
As well in case of custom types like Students
List<Students> val = new List<Students>();
val.Add(new Students { Name = "aanand", Age = 22,City="Hyd" });
val.Add(new Students { Name = "balu", Age = 24,City="Pune" });
val.Add(new Students { Name = "charan", Age = 19,City="Bangalore" });
val.Add(new Students { Name = "dinesh", Age = 19, City="Hyd"});
or
// List<Students> val = new List<Students>(IEnumerable ier)
IEnumerable<Students> t = val.Where((Students, s1) => (Students.City == "Hyd"));----2nd predicate

--àt.Where(Fun<Student,int,bool>);

IEnumerable<Students> t = val.Where(Students => (Students.City == "Hyd"));
       --- 1st predicate
--àt.Where(Fun<Student,bool>,predicate);
And
1st predicate
(Students.City == "Hyd")-àreturn True/False.


Early loading/eagar loading:
index % 2 != 0
true
index % 2 != 0
false
index % 2 != 0
true
index % 2 != 0
false
index % 2 != 0
true

if students belongs to Hyd returns true other wise false.
2nd predicate
Val.Where((students,s1)=>(Students.City==”Hyd”));
if students belongs to Hyd returns true other wise false.
   foreach (Students item in t)
   {
       Console.WriteLine(item.Name+" "  +item.Age+"   "+item.City);
   }
Output:
1,3,8,21,55---intarray
Charan-----stringarray
Anand 22 Hyd
Dinesh 19 Hyd


s.Contains("charan")
false
s.Contains("charan")
false
s.Contains("charan")
true
s.Contains("charan")
false
s.Contains("charan")
The name 's' does not exist in the current context

above 2 are eagar/early loading.

Select Predicate:
List<Students> val = new List<Students>();
            val.Add(new Students { Name = "aanand", Age = 22, City = "Hyd" });
            val.Add(new Students { Name = "balu", Age = 24, City = "Pune" });
            val.Add(new Students { Name = "charan", Age = 19, City = "Bangalore" });
            val.Add(new Students { Name = "dinesh", Age = 19, City = "Hyd" });
   
 IEnumerable<Students> stuvalues = val.Select(r => r);



            foreach (Students item in stuvalues)
            {
                Console.WriteLine(item.City);
            }
2nd predicate:
IEnumerable<string> stuvalues = val.Select(r => r.City);

or
IEnumerable<string> stuvalues = val.Select<Students, string>(r => r.City);
    foreach (string item in stuvalues)
   {
                Console.WriteLine(item.City);
   }

but

Lazy loading/deffered loading

Students.City=="Hyd"
false
Students.City=="Hyd"
True



Both perdicates: where and select

IEnumerable<Students> stuvalues = val.Where((Students, s1) => (Students.City == "Hyd")).Select(r => r);

or


IEnumerable<Students> stuvalues = val.Where((Students, s1) => (Students.City == "Hyd"));//.Select(r => r);

            foreach (Students item in stuvalues)
            {
                Console.WriteLine(item.City);
            }
IEnumerable<string> stuvalues = val.Where((Students, s1) => (Students.City == "Hyd")).Select(r => r.City);
            foreach (string item in stuvalues)
            {
                Console.WriteLine(item);
            }
Note: projection operator can be where/select.but the return type is should math
both the sites

we can know the return type of Linq query while showing

like

before writing select projection in linq query




after writing select projection in linq query

No comments:

Post a Comment