Tuesday, 13 September 2011

LINQ with asp.net

.Net 3.5 introduced a new techonolgy  called Language Integrated Query or Linq(pronounced as "link")
LINQ includes three basic type of queries:
LINQ to Object;
LINQ to XML
and LINQ  used in the context of database ,like LINQ to SQL or LINQ  to ENTITES

LINQ to Object;

Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Movie
/// </summary>
public class Movie
{
    public string Title { get; set; }
    public string Director { get; set; }
    public int Genre { get; set; }
    public int RunTime { get; set; }
    public DateTime ReleaseDate{get;set;}

    public Movie()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var mov = GetMovies();
        var qu = new List<Movie>();
        foreach (var m in mov)
        {
            if (m.Title =="Ram")
                qu.Add(m);
        }


        GridView1.DataSource = qu;
        GridView1.DataBind();
    }


    public List<Movie> GetMovies()
    {
    return new List<Movie> {new Movie {Title ="Ram", Director ="ABHI" ,Genre =0, ReleaseDate =DateTime.Parse ("05/12/2001"),RunTime =89},
        new Movie {Title ="Gopal",Director ="Rohit",Genre =1,ReleaseDate =DateTime .Parse ("4/12/2002"),RunTime =96},new Movie {Title ="Ram",Director ="AMIT", Genre =1,ReleaseDate =DateTime .Parse ("04/12/2011"),RunTime =34}
    };
   
    }
 

  

}

 Output:-
TitleDirectorGenreRunTimeReleaseDate
RamABHI0895/12/2001 12:00:00 AM
RamAMIT1344/12/2011 12:00:00 AM












No comments:

Post a Comment