Shalvin Interests

Friday, February 7, 2014

Linq to Objects

This blog is a continuation of http://shalvinpd.blogspot.in/2008/12/linq-to-object.html.


Or |
List<string> glsAspNet = new List<string> { "Shalvin", "Manoj", "Simil", "Tina" };

 protected void btnContains_Click(object sender, EventArgs e)
 {
    var C = from p in glsAspNet
            where p.Contains("a") | p.StartsWith("S")
            select p;

    ListBox1.DataSource = C;
    DataBind();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
List<string> obj = new List<string>{"Anjali", "Anju", "Ammu", "athira", "Minu",
    "gayathri", "gopu", "anu"};
protected void Page_Load(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
//    var Astart = from p in obj where p.StartsWith("A") select p;
//    ListBox1.DataSource = Astart;
//    DataBind();

}
protected void btnContains_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = from p in obj
                where p.Contains("a")
                select p;
    ListBox1.DataSource = res;
    DataBind(); 
}
protected void btnEndsWith_Click(object sender, EventArgs e)
{
        
    ListBox1.Items.Clear();
    var res = from p in obj
                where p.EndsWith ("a")
                select p;
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = (from p in obj
                select p).First();
    //ListBox1.DataSource = res;
    //DataBind();
    Response.Write(res);
}

protected void Button4_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = from p in obj
                where p.StartsWith("A") | p.StartsWith("g")
                select p;
    ListBox1.DataSource = res;
    DataBind();
       
}
protected void btnAnd_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = from p in obj
                where p.StartsWith("A") & p.EndsWith("a")
                select p;
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnOrderBy_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = from p in obj
                orderby p
                select p;
    ListBox1.DataSource = res;
    DataBind();
}

protected void btnOrderByDescending_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = from p in obj
                orderby p descending
                select p;
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnContainsOrderBy_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = from p in obj
                where p.Contains("a")
                orderby p 
                select p;
    ListBox1.DataSource = res;
    DataBind();
}


protected void Button9_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = from p in obj
                where p.Contains("a")|p.StartsWith("A")
                orderby p
                select p;
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnTake_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = (from p in obj
                select p).Take(5);
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnTake2_Click(object sender, EventArgs e)
{

    ListBox1.Items.Clear();
    var res = (from p in obj where p.StartsWith("A")
                select p).Take(5);
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnSkip_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = (from p in obj
              select p).Skip(5);
    ListBox1.DataSource = res;
    DataBind();
}
protected void btnStartsWith2_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    var res = obj.Where(p => p.StartsWith("A"));
    ListBox1.DataSource = res;
    DataBind();
}
}


Linq and Collection of Class

List<Trainee> glTrainee = new List<Trainee>
{
 new Trainee{Name = "Rejitha", Location = "Varkala", Hobby ="Music"},
 new Trainee{Name = "Sarath", Location = "Sub Station", Hobby = "Reading"},
 new Trainee{Name = "Anish", Location = "Sub Station", Hobby= "Swimming"},
  new Trainee{Name ="Anoop" , Location ="Attinkuzhi", Hobby="Blogging"},
   new Trainee{Name = "Vishnu", Location ="Front Gate", Hobby = "Photography"},
    new Trainee{Name ="Sanal", Location ="Police Station", Hobby = "Talking"},
    new Trainee{Name = "Manikandan", Location = "Police Station", Hobby = "Cricket"},
     new Trainee{Name = "Saritha", Location = "Sub Station", Hobby="Reading"},
};
protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
   var Loc = glTrainee.Select(p => p.Location).Distinct().ToList();
    ddlLocation.DataSource = Loc;
    DataBind();
 }
}
protected void btnSubStation_Click(object sender, EventArgs e)
{
 var S = from p in glTrainee
         where p.Location == "Sub Station"
         select p;
 GridView1.DataSource = S;
 DataBind();

}
protected void btnStartsWithA_Click(object sender, EventArgs e)
{
 var s = glTrainee.Where(p => p.Name.StartsWith("A"));

 GridView1.DataSource = s;
 DataBind();
}
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
 var s = from p in glTrainee
         where p.Location == ddlLocation.Text
         select p;
 GridView1.DataSource = s;
 DataBind();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
 var s = from p in glTrainee
         select new {p.Name, p.Location};
GridView1.DataSource = s;
 DataBind();
}
protected void btnSortByLocation_Click(object sender, EventArgs e)
{
 var s = from p in glTrainee
         orderby p.Location
         select p;
  GridView1.DataSource = s;
 DataBind();
}
protected void btnSortByLocationThenByName_Click(object sender, EventArgs e)
{
 var s = from p in glTrainee
         orderby p.Location, p.Name
         select p;
 GridView1.DataSource = s; 
 DataBind();
}
protected void btnSingle_Click(object sender, EventArgs e)
{
 //var s = from p in glTrainee
 //        where p.Name == "Manikandan"
 //select p;

 var s = (from p in glTrainee
         where p.Name == "Manikandan"
         select p).Single();

 Label1.Text = s.Name;
 //GridView1.DataSource = s;
 //DataBind();
}
protected void btnSingleOrDefault_Click(object sender, EventArgs e)
{
 var s = (from p in glTrainee
          where p.Name == "Manikandans"
          select p).SingleOrDefault();
 if( s != null)
 Label1.Text = s.Name;

}

No comments:

Post a Comment