Monday, December 8, 2008

Linq to Objects

Linq (Language Integrated Query) enables you to have queries within the language.
This blog assumes that you are know Type Inference in C#, Generics and Collection Initializer.

private void Form1_Load(object sender, EventArgs e)
{
List<string> interests = new List<string>{"Music", "Singing", "Swimming"};
var result = from w in interests
where w.Contains("ing")
select w;

foreach (string s in result)
listBox1.Items.Add(s);
}


Lambda
protected void btnLambda_Click(object sender, EventArgs e)
{
   List<string> interests = new List<string>{"Music", "Singing", "Swimming"};
   var result = interests.Where(p => p.Contains("ing"));
        
   foreach (string s in result)
     ListBox1.Items.Add(s);
}


The Linq query retrives all values form Generic List that ends with "ing". You can use Linq with any objects that implements IEnumerable or Generic IEnumerable interface.

StartsWith
private void btnStartsWith_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = from p in interests
            where p.StartsWith("M")
            select p;
foreach (string s in ms)
    listBox1.Items.Add(s);
}

EndsWith
private void btnEndWith_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = from p in interests
            where p.EndsWith("g")
            select p;
foreach (string s in ms)
    listBox1.Items.Add(s);
}


Equals
private void btnEquals_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = from p in interests
            where p.Equals("Music")
            select p;
foreach (string s in ms)
    listBox1.Items.Add(s);
}


First
First can be used to extract only the first item in the collection.
private void btnFirst_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = (from p in interests
            select p).First();
listBox1.Items.Add(ms);
}

Last
private void btnLast_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = (from p in interests
            select p).Last();
listBox1.Items.Add(ms);
}

Count
Using the Count operator I am taking a counts of items inside the Generic List. I will get a value 3.

private void btnCount_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = (from p in interests
            select p).Count();
listBox1.Items.Add(ms);
}

Count of EndsWith
Here I am taking a count of items ending with "ing". I will get an output of 2 ie. the count of "Swimming" and "Singing".

private void btnCountEndWithIng_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = (from p in interests
          where p.EndsWith("ing")  
          select p).Count();
listBox1.Items.Add(ms);
}

Count Equals

private void btnCountEquals_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var ms = (from p in interests
where p.Equals("Music")
          select p).Count();
listBox1.Items.Add(ms);
}

Linq and Generic List of Integer
Having seen working with Generic List of string let's turn out attention to Generic List of Inteters. 


List<int> gliMarks = new List<int> { 98, 56, 88, 79, 96 };

GreaterThan Operator

private void btnShowGreaterThan90_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var G90 = from p in gliMarks
    where p > 90
    select p;
listBox1.ItemsSource = G90;
}


Sum

private void btnSum_Click(object sender, RoutedEventArgs e)
{
    listBox1.Items.Clear();
           
    var vSum = (from p in gliMarks
                select p).Sum();
    listBox1.Items.Add(vSum);
}



Average

private void btnAverage_Click(object sender, RoutedEventArgs e)
{
    listBox1.Items.Clear();
    var Avg = (from p in gliMarks
                    select p).Average();
    listBox1.Items.Add(Avg);
}

Max

private void btnMaximum_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var Maxi = (from p in gliMarks
            select p).Max();
listBox1.Items.Add(Maxi);
}

Or

private void btnSOr_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
var vOr = from p in gliMarks
    where p== 98 | p == 88 
    select p;
listBox1.ItemsSource = G90;
}



Linq on Collection of Objects

class Session
{
public string Speaker { get; set; }
public string SessionName { get; set; }
}

List<Session> glSessions = new List<Session>{
new Session{Speaker = "Shalvin P D", SessionName= "Entity Framework 4"},
new Session{Speaker="Praseed Pai", SessionName="Powershell"}};

var G = from p in glSessions
where p.Speaker == "Shalvin P D"
select new { p.Speaker, p.SessionName };


dataGrid1.ItemsSource = G.ToList();
}

No comments:

Post a Comment