Sunday, 2 February 2014

Let clause in Linq to Sql

Let clause in Linq to Sql........................

Let is a part of a query expression. It introduces a variable. We can then reuse the variable elsewhere in the query. This makes possible certain complex query expressions, and makes other expressions simpler.

When to use let. You only need to use the let keyword if you are introducing a new variable into the query expression that must be computed and also reused. In the query expression above, the variable v is used twice.

Example let clause in Linq to Sql…………………………………

1.Note : In this example  we will find these numbers  which is even
 
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
{
protected void Page_Load(object sender, EventArgs e)
{
}
string number = "";

protected void btngetdata_Click(object sender, EventArgs e)
{

int[] no = { 22, 45, 67, 12, 87, 45, 23,  90, 56, 34, 23, 67 };
var evenodd = from n in no
let v = n % 2
where v == 0
select n;

foreach (var nn in evenodd)
{
number += nn + ",";
}
lblmessage.Text ="Even Number is= "+ number.Remove(number.Length-1);

}
}

 Output   Even Number is= 22,12,90,56,34


2.Note : In this example  we will find these month name  which character length is grater than 5


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
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void btngetdata_Click(object sender, EventArgs e)
{

var months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

var result = from m in months
let len = m.Length
where len > 5
orderby len
select m;

//or
var result1 = months.Select(monthname =>
new { namelength = monthname.Length, monthname })
.Where(x => x.namelength > 5)
.OrderBy(x => x.namelength)
.Select(x => x.monthname);

GridView1.DataSource = result;
GridView1.DataBind();

}
}

Output   Output  is = August,January,October,February,November,December,September

**************************************************************


--sql query………………….


create table Employee(SrNo int primary key,Name varchar(50) unique,Dob Date,Salary int)
select SrNo,Name,Dob,Salary,Age=DateDiff("yy",Dob,GetDate())from Employee

3.Note : In this example  we will find those Employees who  have 30 years old from 15 years old

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
{
protected void Page_Load(object sender, EventArgs e)
{

}
DataClassesDataContext dd = new DataClassesDataContext();

protected void btngetdata_Click(object sender, EventArgs e)
{

var v = from m in dd.Employees
                let l = (DateTime.Now - (DateTime)m.Dob).TotalDays
                where l < (365 * 27) && l > (365 * 14)
                select new { m.SrNo, m.Name,m.Dob, m.Salary };
        GridView1.DataSource = v;
        GridView1.DataBind();

}
}

Output......................................................................





0 comments:

Post a Comment