Wednesday, April 2, 2014

LINQ With Lambda Expressions

LINQ With Lambda Expressions

  • LINQ queries can be written in two syntaxes:
    1.  General Query Syntax
    2.  Lambda Expression Syntax
  • The previously written applications are written with General Query Syntax.
  • Syntax for Lambda Expression:
DataSource.Clause(DataAliasName => Expression)
  • Ex:  stu.Where(s => s.Marks < 300)
Program with LINQ to LAMBDA EXPRESSIONS
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQtoObjectsDemo
{
   class Student
    {
      //fields
        public int StudentID;
        public string Name;
        public string Course;
        public int Marks;
     //constructor
        public Student(int StudentID, string Name, string Course, int Marks)
         {
            this.StudentID = StudentID;
            this.Name = Name;
            this.Course = Course;
            this.Marks = Marks;
        }
    }
}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQwithLambaExpressions
{
  class Program
   {
     static void Main(string[] args)
      {
        //data source
          Student[] stu = { new Student(101, "Prakash", "MBA", 765),
                                    new Student(102, "Pradeep", "MBA", 471),
                                    new Student(103, "Pushpa", "Msc", 590),
                                    new Student(104, "Purna", "MCA", 223),
                                     new Student(105, "Purnima", "MCA", 450)};
       //linq query with where clause
         IEnumerable result1 = stu.Where(s => s.Course == "MCA");
         Console.WriteLine("MCA Students:");
         foreach (Student r in result1)
         Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks);
       //linq query with compound where clause
         IEnumerable result2 = stu.Where(s => s.Name.EndsWith("a") && s.Marks >= 400 &&                                                                 s.Marks <= 600);
         Console.WriteLine("\nStudents whose name ends with 'a', and marks is >=400 and <=600:");
         foreach (Student r in result2)
         Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks);
       //linq query with orderby clause
         IEnumerable result4 = stu.OrderBy(s => s.Marks);
         Console.WriteLine("\nStudents (sort on marks):");
         foreach (Student r in result4)
         Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks);
       //linq query with orderby clause (descending)
         IEnumerable result5 = stu.OrderByDescending(s => s.Marks);
         Console.WriteLine("\nStudents (sort on marks - descending):");
         foreach (Student r in result5)
         Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks);
       //linq query with group clause
         IEnumerable> result6 = stu.GroupBy(s => s.Course);
          Console.WriteLine("\nStudents with grouping:");
          foreach (IGrouping> StuGrp in  result6)
           {
             Console.WriteLine(StuGrp.Key + ":");
              foreach (Student r in StuGrp)
             Console.WriteLine(" " + r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks);
            }
            Console.Read();
        }
    }
}
Previous Page : LINQ

No comments:

Post a Comment

Flag Counter