Friday, May 1, 2009

Introduction to LINQ in .Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PubishApps
{
public class IntroWithLINQ
{
/************************************************
* Topic : Introduction to LINQ in .Net
* Reference Required: System.Linq.
* Author : kalit sikka
* Summary: This article include different methods to show some simple features of LINQ that could make a life of a developer quit easlier in his day-to-day work.
* **********************************************/
public List GetDeveloperList()
{
List oList = new List();
oList.Add(new Developer(1001, "Sri Ram", Designation.Project_Lead, 10));
oList.Add(new Developer(1020, "Amit Narang", Designation.Team_Lead, 8));
oList.Add(new Developer(1056, "Mohit singh", Designation.Sr_Software_Engineer, 7));
oList.Add(new Developer(1081, "Kalit Sikka", Designation.Sr_Software_Engineer, 6));
oList.Add(new Developer(1102, "Sunil Mittal", Designation.Software_Engineer, 4));
oList.Add(new Developer(1106, "Jaswinder saini", Designation.Software_Engineer, 3));
oList.Add(new Developer(1108, "Varinder Pal", Designation.Software_Engineer, 3));
return oList;
}
///
/// Simple LINQ method using where clause
///

public void Linq_WhereClause() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums =
from n in numbers
where n < 5
select n;
Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums) {
Console.WriteLine(x);
}
}
///
/// Using where clause on List object
///

public void ListWithWhere()
{
List oDeveloper = GetDeveloperList();
var Experienced =
from d in oDeveloper
where d.Experience > 5
select d;
Console.WriteLine("Experienced Developers");
foreach (var Developer in Experienced) {
Console.WriteLine(string.Format("Name {0} - Experience {1}", Developer.Name, Developer.Experience));
}
}
///
/// Using where clause and And clause on List object
///

public void ListWithWhereClause_AndClause() {
List oDeveloper = GetDeveloperList();
var ExperiencedSeniorDeveloper =
from d in oDeveloper
where d.Experience > 5 && d.eDesgination == Designation.Sr_Software_Engineer
select d;
Console.WriteLine("Senior Experienced Developers");
foreach (var Developer in ExperiencedSeniorDeveloper) {
Console.WriteLine(string.Format("Name {0} - Experience {1} and Desgination {2}", Developer.Name, Developer.Experience, Developer.eDesgination));
}
}
///
/// Using Lambda in Linq on List
///

public void LinqWithLambda() {
List oDeveloper = GetDeveloperList();
//Lambda expressions allow us to write functions that can be passed
//as arguments to methods, for example, to supply predicates for
//subsequent evaluation.
var MaxExperience = oDeveloper.Where(x => x.Experience > 5).OrderBy(x => x);
Console.WriteLine("Most Experienced Developer");
foreach (var Developer in MaxExperience) {
Console.WriteLine(string.Format("Name {0} - Experience {1} and Desgination {2}", Developer.Name, Developer.Experience, Developer.eDesgination));
}
}
///
/// Add info into List object
///

public void AddValuesIntoList() {
List oDeveloper = GetDeveloperList();
var AddOneYearofTrainingInExperience =
from d in oDeveloper
select d.Experience+1;
Console.WriteLine("Extended Experienced");
foreach (var Developer in AddOneYearofTrainingInExperience) {
Console.WriteLine(string.Format("Name {0} - Experience {1}", Developer.Name, Developer.Experience));
}
}
///
/// Changing Case
///

public void ChangeTheCaseOfText() {
string[] words = { "aMit", "BalWinDer", "cHeRry", "haRRY", "NaraGh" };
var upperLowerWords =
from w in words
select new {Upper = w.ToUpper(), Lower = w.ToLower()};
foreach (var ul in upperLowerWords) {
Console.WriteLine("Uppercase_Names: {0}, Lowercase_Names: {1}", ul.Upper, ul.Lower);
}
}
///
/// Giving Alias name to fields
///

public void GivingAliasNames()
{
List oDeveloper = GetDeveloperList();
var ChangeNames =
from d in oDeveloper
select new {Developer_Name = d.Name, Developer_Desg = d.eDesgination};
Console.WriteLine("Developer Details");
foreach (var Developer in ChangeNames)
{
Console.WriteLine(string.Format("Name {0} - Desgination {1}", Developer.Developer_Name, Developer.Developer_Desg));
}
}
///
/// Getting values from Differnet arrays
///

public void GetValueFromArrays() {
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var pairs =
from a in numbersA
from b in numbersB
where a < b
select new {a, b};
Console.WriteLine("Pairs where a < b:");
foreach (var pair in pairs) {
Console.WriteLine("{0} is less than {1}", pair.a, pair.b);
}
}
///
/// Skipping the values from the collection
///

public void Linq22() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var allButFirst4Numbers = numbers.Skip(4);
Console.WriteLine("All but first 4 numbers:");
foreach (var n in allButFirst4Numbers) {
Console.WriteLine(n);
}
}
///
/// TakeWhile to return elements starting from the
/// beginning of the array until a number is hit that is not less than 5
///

public void Linq2TakeWhile() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 5);
Console.WriteLine("First numbers less than 5:");
foreach (var n in firstNumbersLessThan6) {
Console.WriteLine(n);
}
}
///
/// Sorting Values in List
///

public void SortingVaues()
{
List oDeveloper = GetDeveloperList();
var SortByDesgAndExperience =
from d in oDeveloper
orderby d.eDesgination, d.Experience descending
select d;
Console.WriteLine("Sorted Data");
foreach (var Developer in SortByDesgAndExperience) {
Console.WriteLine(string.Format("Name {0} - Experience {1}", Developer.Name, Developer.Experience));
}
}
///
/// Getting distinct values from the array
///

public void DistinctValues() {
int[] vals = { 2, 4, 7, 9, 5, 4, 2, 2 , 2, 3, 5, 5 };
var unique = vals.Distinct();
Console.WriteLine("Distinct Values:");
foreach (var v in unique) {
Console.WriteLine(v);
}
}
///
/// Union of two arrays
///

public void UnionLINQ() {
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var uniqueNumbers = numbersA.Union(numbersB);
Console.WriteLine("Unique numbers from both arrays:");
foreach (var n in uniqueNumbers) {
Console.WriteLine(n);
}
}
///
/// First match from the List
///

public void FirstMatch() {
List oDeveloper = GetDeveloperList();
var FirstMatch =
(from d in oDeveloper
where d.Experience > 2 && d.eDesgination == Designation.Software_Engineer
select d).First();
Console.WriteLine("FirstMatch");
Console.WriteLine(string.Format("Name {0}", FirstMatch.Name));
}
}
public class Developer
{
public int ID;
public string Name;
public Designation eDesgination;
public WorkStatus eWorkStatus;
public double Salary;
public string ProjectManager;
public int Experience;
public Developer(int ID, string Name)
{
this.ID = ID;
this.Name = Name;
}
public Developer(int ID, string Name, Designation eDesgination, int Experience)
{
this.ID = ID;
this.Name = Name;
this.eDesgination = eDesgination;
this.Experience = Experience;
}
}
public enum WorkStatus
{
ProjectInHand,
OnBench
}

public enum Designation
{
Software_Engineer,
Sr_Software_Engineer,
Team_Lead,
Project_Lead
}

}

No comments:

Post a Comment

Locations of visitors to this page