Monday, May 16, 2011

LINQ Lessons

In our recent project, we came across many situations where we had to write queries on our objects in the UI tier. Few of them are give below.

I am taking here the movie class to explain different Linq queries.
public class Movie
{
     public int ID {get; set;}
     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 decimal Budget{get; set;}

}

public List GetMovies()
{
return new List {
new Movie{Title="Rocky", Director="Alex", Genre=1, RunTime=180, ReleaseDate ="01/01/1990"},
new Movie{Title="Return of the Devil", Director="Michael", Genre=2, RunTime=80, ReleaseDate ="01/01/1998"},
new Movie{Title="Warriors", Director="Phil", Genre=3, RunTime=120, ReleaseDate ="01/01/1991"},
new Movie{Title="Love", Director="Kevin", Genre=4, RunTime=120, ReleaseDate ="01/01/1992"},
new Movie{Title="Live Forever", Director="Leonardo", Genre=5, RunTime=120, ReleaseDate ="01/01/1997"},
new Movie{Title="War", Director="Adams", Genre=6, RunTime=130, ReleaseDate ="01/01/2001"},
new Movie{Title="Crimes of the City", Director="Ricky", Genre=6, RunTime=130, ReleaseDate ="01/01/1995"},
new Movie{Title="Street Fighters", Director="Andrew", Genre=6, RunTime=130, ReleaseDate ="01/01/1995"},
new Movie{Title="Violence", Director="David", Genre=6, RunTime=130, ReleaseDate ="01/01/1998"},
new Movie{Title="College Days", Director="Clarke", Genre=4, RunTime=140, ReleaseDate ="01/01/1997"},

};
}


Query: Get all movies from generic movies collection.
var movies = GetMovies();
var query = from m in movies select m;

Query : Return a new projection containing Title and Genre.
var movies = GetMovies();
var query = from m in movies select new {m.Title, m.Genre};
or
var query = from m in movies select new {MovieTitle = m.Title, MovieGenre = m.Genre};

Query: Return all movies order by Title
var movies = GetMovies();
var query = from m in movies order by m.Title descending select m

Query: Adding a filter to a Linq query.
var movies = GetMovies();
var query = from m in movies where m.Genre=5 select m;

Query: Data Grouping
var movies = GetMovies();
var query = from m in movies group by m.Genre into g
            select new {Genre = g.key, Count = g.Count} 
            

Query : Linq Operators
var movies = GetMovies();
movies.Max(m=>m.RunTime)
movies.Min(m=>m.RunTim)
movies.Average(m=>m.RunTime)

Query: Linq Joins
var moveis = GetMovies();
var genres = GetGenres();

var query = from m in movies Join g in genres On m.Genre Equals g.ID
            select new {Title = m.Title, Genre = g.Name}
Query: Left Join
All movies from movies object irrespecitive of having a genre or not.

var m = from m in movies join g in generes on m.Genre equals g.ID into MovGen
              from MovGenJ in MovGen.DefaultIfEmpty()
             select new {Title = m.Title, Genre = g.Name}

Query: Paging using Linq

var query = (from m in movies Join g in generes On m.Genre Equals g.ID 
             select new {Title = m.Title, Genre = g.Name}).Skip(10).Take(10);
Query: For loop in Linq

var m = movies.ForEach(m =>m.Budget = GetMoviesBudget(m.ID))

No comments:

Post a Comment