Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Friday, August 29, 2008

[Tutorial] LINQ data query language
Part 1 - Setting up a project

This is the first part of a series that is dedicated to LINQ, which is a new data query language that was introduced with .NET framework 3.0. It facilitates and generalizes working with different kinds of data sources (LINQ To SQL, LINQ To XML, LINQ To Entities, etc).

This part shows how to configure a project for using LINQ and serves as fundament for all following parts. A console project is used for simplification but LINQ can be used in any type of project. The AdventureWorksDB is used for all data queries.

  • Add a new data connection to the project using the Server Explorer

  • Add a new item of type LINQ to SQL Classes to your project.

  • Some new files will be auto-generated and added to the project.

  • Open the dbml file and add some tables from the data source using the Server Explorer by dragging them to the design view of the dbml file.


  • Initialize an object of the auto-generated AdventureWorksDataContext class and display some product data.

  • Run the application and you will get the following output.


Share/Save/Bookmark

Wednesday, August 27, 2008

[Tool] LINQPad - Query Analyzer for LINQ queries

Visual Studio does not contain any sort of query analyzer for LINQ at the moment. You have to manually write test code if you want to test your queries before implementing them in your projects. LINQPad fills this gap and adds several additional features that you soon don't want to miss anymore.

The download for this tool can be found here:
http://www.albahari.com/LINQPad.exe

The homepage with further instructions for this tool can be found here:
http://www.linqpad.net

  • The GUI has the look-and-feel of traditional query analyzers

  • You can write and test your queries in different proamming languages including C#, VB and SQL

  • You can display the LINQ processing instructions as well as the SQL queries that are executed in the background




Share/Save/Bookmark

Saturday, August 16, 2008

[Tutorial] ADO.NET Entity Framework
Part 3 - Accessing data and using LINQ (2/2)

The last part showed how to use LINQ in combination with the Entity Framework. This part explains how to trace the automatically generated SQL statements that are executed in the background. You need to have the correctly configured project from the last part as prerequisite for the following examples.

  • Cast the query object into an "ObjectQuery<Product>" for being able to access the "ToTraceString()" function.

  • Run the application and you will get the following output.

  • Now change the LINQ query so that there are additional constraints.

  • Run the application again to compare the differences in the generated SQL statements.


  • Now add a Lambda expression to see what type of SQL statements are generated in this case.

  • Run the application and you will get the following output.



Share/Save/Bookmark

Friday, August 15, 2008

[Tutorial] ADO.NET Entity Framework
Part 2 - Accessing data and using LINQ (1/2)

The last part of the series described how to set up a project for using the Framework. This part shows how to easily query information from the configured data sources. You need to have a console project with an ADO.NET data model that links to the AdventureWorks DB as prerequisite for the following examples.

The first example shows how to easily display information from within the DB with very little code.


  • Initialize an object of the auto-generated AdventureWorksEntities class and display some product data.

  • Run the application and you will get the following output.


This is the easiest but also the most inflexible way of accessing data from the configured entities. It will not really be sufficient if you need more control over the data. Using LINQ will give you everything necessary to really have maximum flexibility.

  • The following example displays the same results as the last example but this time by using LINQ.

  • A good practice is to externalize the creation of the LINQ query for better extensibility, readability and understanding. I also restricted the result to have just 20 elements since we don't need to query all products.

  • Run the application and you will get the following output.


Share/Save/Bookmark