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.
* For : http://eggheadcafe.com
* **********************************************/
public List<Developer> GetDeveloperList()
{
List<Developer> oList = new List<Developer>();
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;
}
/// <summary>
/// Simple LINQ method using where clause
/// </summary>
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);
}
}
/// <summary>
/// Using where clause on List object
/// </summary>
public void ListWithWhere()
{
List<Developer> 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));
}
}
/// <summary>
/// Using where clause and And clause on List object
/// </summary>
public void ListWithWhereClause_AndClause() {
List<Developer> 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));
}
}
/// <summary>
/// Using Lambda in Linq on List
/// </summary>
public void LinqWithLambda() {
List<Developer> 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));
}
}
/// <summary>
/// Add info into List object
/// </summary>
public void AddValuesIntoList() {
List<Developer> 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));
}
}
/// <summary>
/// Changing Case
/// </summary>
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);
}
}
/// <summary>
/// Giving Alias name to fields
/// </summary>
public void GivingAliasNames()
{
List<Developer> 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));
}
}
/// <summary>
/// Getting values from Differnet arrays
/// </summary>
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);
}
}
/// <summary>
/// Skipping the values from the collection
/// </summary>
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);
}
}
/// <summary>
/// TakeWhile to return elements starting from the
/// beginning of the array until a number is hit that is not less than 5
/// </summary>
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);
}
}
/// <summary>
/// Sorting Values in List
/// </summary>
public void SortingVaues()
{
List<Developer> 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));
}
}
/// <summary>
/// Getting distinct values from the array
/// </summary>
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);
}
}
/// <summary>
/// Union of two arrays
/// </summary>
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);
}
}
/// <summary>
/// First match from the List
/// </summary>
public void FirstMatch() {
List<Developer> 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));
}
/// <summary>
/// Use of Except IEnumerable method to compare two sequences of integers and
/// return elements that appear only in the first sequence.
/// </summary>
public void ExceptInLINQ()
{
int[] numbers1 = { 2, 7, 9, 8, 4, 6, 3, 5 };
int[] numbers2 = { 7, 3 };
IEnumerable<int> onlyInFirstSet = numbers1.Except(numbers2);
foreach (int number in onlyInFirstSet)
Console.WriteLine(number);
}
}
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
}
}