..


Sponsored Links

Querying data with LINQ

With the latest versions. NET Framework is a new technology was introduced at the level of interaction with the databse called LINQ (Language Integrated Query). This is a set of extensions. NET Framework to perform fast queries on the data. LINQ extends the syntax of C # and Visual Basic, but not replace other technologies for data access, it is a useful alternative to the same.

This technology is called 'language integrated' because it offers the opportunity to build the query you want to integrate them into the syntax of programming languages ​​defined as C # and Visual Basic.

To understand the potential we proceed with an example. Add a new web form to our project and insert a GridView control inside it for viewing the data obtained from the LINQ query that will perform in the near future. In this example we will use as a data source class cars that we have seen a few lessons ago (the house by adding another model Ford Focus).

In the Load event of the form insert the following code






 if (! this.IsPostBack)







 {



  



 List <Auto> autoList Auto.CreaListaAuto = ();



  



 = GridView1.DataSource from car autoList



  



 where



  



 auto.Marca.Contains ("Ford") == true



  



 orderby auto.Marca.Length



  



 auto.Modello.ToUpper select ();



  



 GridView1.DataBind ();







 }



With this syntax we are asking to bring back to the grid only Ford brand car models running the application and in fact the result will be displayed the following

If we modify the code to display the brand we also write






 protected void Page_Load (object sender, EventArgs e)







 {



  



 if (! this.IsPostBack)



  



 {



    



 List <Auto> autoList Auto.CreaListaAuto = ();



    



 = GridView1.DataSource from car autoList



    



 where



    



 auto.Marca.Contains ("Ford") == true



    



 orderby auto.Modello.Length



    



 select car;



    



 GridView1.DataBind ();



  



 }







 }



and the result is as follows

Summing up the size of a LINQ statement is as follows






 <variabile from a collezione> in <collezione>

 





 <criteri where the SELECT

 





 orderby <criteri of ordinamento>







 <object select items selezionati>



The most interesting thing is that we can query with LINQ (but also modify the data) using a model independent of the various types of sources. It 'can in fact access to databases, text files, XML files, arrays, Excel files, configuration files, information about assemblies, registry keys and other objects traceable to a collection of enumerable objects.

There are several variants of this technology, including:

  • LINQ to Objects - Allows you to run queries on collections of objects in memory
  • LINQ to XML - Allows you to perform operations on information in XML format
  • LINQ to DataSet - Allows you to query Typed DataSets
  • LINQ to SQL - Allows to represent a graph of objects in memory that represent the objects in a SQL Server database, which then execute the query

In particular, the technology used is LINQ to SQL to deepen what I would refer you to an article I wrote some time ago that you can find on this page .

Help with Visual Studio ASP.Net
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
ASP.NET (Course) ASP.NET (Course)
Full course for building Web applications from 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links