LINQ (Language-Integrated Query) is a new data-access feature that enables the querying of data sources from within the language itself. Typically in .NET, we use a technology such as ADO.NET to directly query a relational database management server, such as SQL Server or MySQL. LINQ is a new language construct that allows us to query relational data from different sources and interact with it just like any other object or class. With LINQ, we have access to compile-time syntax checking,
the use of IntelliSense, and the ability to access other data sources such as XML or any custom data sources.
There are many LINQ extensions, such as LINQ-to-SQL, LINQ-to-Sharepoint, LINQ-to-MySQL, LINQ-to-Entities, and so on. Let’s get started with LINQ.
we will use the following object model throughout this tutorial.
List<Movie> movies = new List<Movie>{ new Movie("Scarface",1, 1983), new Movie("The Godfather",1, 1973), new Movie("Carlito's Way",1, 1993), new Movie("Goodfellas",2, 1990), new Movie("Casino",2, 1996), new Movie("Raging Bull",2, 1981) }; List<Actor> actors = new List<Actor> { new Actor(1, "Al", "Pacino"), new Actor(2,"Robert","Deniro") };
The most basic LINQ query retrieves all the object information:
IEnumerable<Actor> allActors = from actor in actors select actor; /*Will return: Actor(1, "Al", "Pacino"), Actor(2,"Robert","Deniro")*/
Our IEnumerable variable called allActors contains the output from the query expression. As you can see, the syntax is similar to SQL.This sample shows the collection of actors in the object model. In this case, we know the return type
var moviesByYear = from movie in movies orderby movie.ReleaseYear select movie;
The above query retrieves all the movie objects in our object model and orders them by year. the var keyword is strongly typed but instead of typing the decleration yourself it lets the compiler determine the type of moviesByYear query expression.
since the return type of the moviesByYear query implements the IEnumerable interface we can use a foreach loop to iterate through the elements.
foreach (Movie a in moviesByYear) { Console.WriteLine(a); }Just like SQL, LINQ has a WHERE clause which can be used to filter results according to the conditions you specify.
var moviesOf1990s = from movie in movies where movie.ReleaseYear >= 1990 select movie; /*will return: Movie("Carlito's Way",1, 1993), Movie("Goodfellas",2, 1990) */
You can also perform joins as you would in SQL:
var movieActorJoin = from movie in movies join actor in actors on movie.ActorId equals actor.Id select new { movie.Title, actor.FirstName, actor.LastName };
since the query result is a collection of custom object we will use the var keyword in the foreach loop to access each element:
foreach (var i in movieActorJoin) Console.WriteLine("{0} {1} {2}",i.Title, i.FirstName, i.LastName); /* Will return: Scarface Al Pacino The Godfather Al Pacino Carlito's Way Al Pacino Goodfellas Robert Deniro Casino Robert Deniro Raging Bull Robert Deniro */
No comments :
Post a Comment