Search content within the blog

Wednesday, May 27, 2009

Introduction to LINQ

The following is an console application that explains the basics of writing queries in LINQ.

Add a new console application to your solution and copy paste the code in a class and run the project. Each section describes different clauses we can use while writing queries. Also it shows the following

1.Use query syntax.
2.Use method syntax.
3.Use a combination of query syntax and method syntax.

The code is self explanatory......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<student> students = new List<student>
{
new student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
new student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
new student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
new student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
new student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
new student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
new student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
new student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
new student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
new student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
new student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
new student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }

};
IEnumerable<student> studentQuery = from student in students
where student.Scores[0] > 90 && (student.Scores[1] > 0 && student.Scores[1] < 90)
orderby student.Last descending
select student;

foreach (student localstudent in studentQuery)
{
Console.WriteLine("First name : {0},lastname :{1},score in paper1:{2}", localstudent.First, localstudent.Last, localstudent.Scores[0]);

}

Console.WriteLine("use of group by");
var studentQuery2 = from objstudents in students
orderby objstudents.Last descending
group objstudents by objstudents.Last[0];


foreach(var localstudentgroup in studentQuery2)
{
Console.WriteLine("Reading from group with key " + localstudentgroup.Key);
{
foreach (student localstudent in localstudentgroup)
{
Console.WriteLine("First name : {0},lastname :{1},score in paper1:{2}", localstudent.First, localstudent.Last, localstudent.Scores[0]);
}

}


}


Console.WriteLine("use of group by with into clause");
var studentQuery3 = from objstudents in students
group objstudents by objstudents.Last[0] into studentGroup
orderby studentGroup.Key ascending
select studentGroup;



foreach (var localstudentgroup in studentQuery3)
{
Console.WriteLine("Reading from group with key " + localstudentgroup.Key);
{
foreach (student localstudent in localstudentgroup)
{
Console.WriteLine("First name : {0},lastname :{1},score in paper1:{2}", localstudent.First, localstudent.Last, localstudent.Scores[0]);
}

}


}

Console.WriteLine("use of let keyword");
var studentQuey4 = from objStudents in students
let totalScore = objStudents.Scores[0] + objStudents.Scores[1] + objStudents.Scores[2] + objStudents.Scores[3]
where totalScore / 4 < objStudents.Scores[0]
select objStudents.First + objStudents.Last;

foreach (string s in studentQuey4)
{
Console.WriteLine(s);
}


Console.WriteLine("use of method syntax");
var studentQuery5 = from objStudent in students
let totalScore = objStudent.Scores[0]
select totalScore;

double averageScoreAtZero = studentQuery5.Average();
Console.WriteLine("Average: " + averageScoreAtZero);


Console.WriteLine("To transform or project in the select clause");
IEnumerable<string> studentQuery6 = from objStudent in students
where objStudent.Last == "Garcia"
select objStudent.First;

foreach (string localStudent in studentQuery6)
{
Console.WriteLine("First name: " + localStudent);
}


Console.WriteLine("Use of annonymous type");
var studentQuery7 = from localStudent in students
let x = localStudent.Scores[0] + localStudent.Scores[1]
where x > 180 && x < 185
select new { id = localStudent.ID, score = x };

foreach (var item in studentQuery7)
{
Console.WriteLine("Id :{0} score {1}", item.id, item.score);
}



Console.WriteLine("Press any key to exit!!!!");
Console.ReadLine();
}
}

public class student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public List<int> Scores;
}


}

No comments:

Post a Comment