Sunday, February 6, 2011

Understand Lambda and anonomous function by reading code

Example code for anonomous function
Before
button1.Click += new EventHandler(button1_Click);
void button1_Click (object sender, EventArgs e)
{
textbox1.Text = DateTime.Now.ToShortDateString();
}

After Lambda Code:
button1.Click += (s, e) => { textbox1.Text = DateTime.Now.TosShortDateString(); } ;

Understand LINQ by reading code (1)

Source
1. Add references:
using System.Data.Linq;
using System.Data.Linq.Mapping;

2. Connect to database:
DataContext db = new DataContext

3. Mapping table and class
[Table(Name = "Customers")]
public class Customer
{
}

4. Query:
Table<Customer> Customers = db.GetTable<Customer>();
IQueryable<Customer> custQuery =
from cust in Customers
where cust.City == "London"
select cust;
foreach (Customer cust in custQuery)
{
Console.WriteLine("ID={0}, City={1}", cust.CustomerID,
cust.City);
}