Tuesday, July 8, 2014

Generate LINQ, SQL queries with LINQPad

Generate LINQ, SQL queries with LINQPad
 
images

Excerpts from msdn about LINQ:
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.
In this article I am sharing how can we generate a LINQ query with a very facilitative tool LINQPad.
The most important fact about LINQPad is, it provides you lot of functionality to play around with tables within a LINQPad editor.
So let’s see how beneficial this is for .Net freaks.
A very initial look of LINQPad shown below:
clip_image002
If you notice there is an option to create a connection with the desired database. I will use Northwind database for my demonstration purpose, here are few sequential steps to make connection establish.
Click in Add connection a window will appear.
clip_image004
Choose “Default (LINQ to SQL) and click on Next button. A New window will appear, fill the required details to get connect with the desired database. As I’ve chosen default provider, (local) server, SQL Authentication option and passed the desired credentials.
clip_image005
Click on Test button and a dialog box will appear with “Connection Successful” message as depicted below:
clip_image006
Click on ok button and see the below image.
clip_image008
To move ahead after the successful connection, now turn is to run some query and see the benefits of this.
Before to run query into a LINQPad editor please have a look on the toolbar section and mouse over on each at least one(toolbar name is very easy to understand).
clip_image010
As you can see into just above window connection is still pointing to none, though connection has been made already, but the open editor is not pointing to any already made connection. Click on connection dropdown and select as your desired as mine is Northwind.
clip_image012
You can also write your query in many modes which .Net supports, See the below image.
clip_image014
I will run inner join query here using Orders and OrderDetails tables of Northwind database.
At very first time I will choose language as SQL and run the following SQL query with green button.
select od.Quantity ,o.OrderID from dbo.Orders as o inner join dbo.OrderDetails as od
on o.OrderID = od.OrderID
Output:
clip_image016
Now I run the following LINQ query into LINQPad editor and choose the Language option as C# Statement(s).
var Result = from o in Orders join od in OrderDetails on o.OrderID equals od.OrderID
select new { od.Quantity,o.OrderID};
I run the above query but nothing comes as result. The reason behind this is we have to use an extension method Dump() to print the result which is built in method of LINQPad.
Let’s run the above query again with Dump method.
var Result = from o in Orders join od in OrderDetails on o.OrderID equals od.OrderID
select new { od.Quantity,o.OrderID};
Result.Dump();
See the output:
clip_image018
There are also some inbuilt extension methods, Right click on any table and see the answer as below showing image.
clip_image020
You can download LINQPad form here http://www.linqpad.net/.
 
You can also have a look on MVC related article here:
Smart Working With Custom Value Providers in ASP.Net MVC
EXECUTION ORDER OF FILTERS IN MVC 4 WITH PRACTICES: IMPORTANT FAQ
Invoke Action With Model Binders in MVC
Extension Helpers Method in MVC
Custom Button With TagBuilder Using MVC Razor Engine
CALL CONTROLLER ACTION METHOD FROM JQUERY USING AJAX
EXECUTION ORDER OF FILTERS IN MVC 4 WITH PRACTICES: IMPORTANT FAQ
MEANING OF SCAFFOLDING IN MVC
REMOVE AMBIGUTY OF CONTROLLER NAMES IN MVC APPLICATION
Precompiled Razor View Using RazorGenerator MVC and PreCompiledViewEngine in MVC 4
So far so good.
Hoe you enjoyed this demonstration.
Keep coding and Be Happy Smile


























































0 comments :

Post a Comment