Type of LINQ ? Part #2


Type of LINQ

There are 3 types of LINQ.

a. LINQ to Object

b. LINQ to XML

c. LINQ to SQL

LINQ to Object Example

It is used to perform complex query operation against any enumerable object

Example of LINQ to Object

Step1:Take a Gridview in aspx page.

Step2:Create a simple generic list of movie objects and bind with gridview like this

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)
{

var movies = GetMovies();
var query = from m in movies
select m;

//Same result will give with this syntax
//var query = movies.Select(m => m);

//var query = from m in movies
// select new { m.Title, m.Director };

//var query = from m in movies
// select new {MovieTile= m.Title,Director= m.Director };

//var query = from m in movies
// orderby m.Title descending
// select new { movieTitle = m.Title, Director = m.Director };

//this syntax is used for filtering

//var query = from m in movies
// where m.Title == “Water” && m.Director == “WWW”
// select m;

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

}

public List<Movie> GetMovies()
{
return new List<Movie>
{
new Movie{Title="Gost",Director="ABC"},
new Movie{Title="Forest",Director="XYZ"},
new Movie{Title="Salt",Director="CDE"},
new Movie{Title="Water",Director="WWW"}
};
}

}

we will get o/p like this

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.