LINQ stands for Language Integrated Query and enables us to query any type of data store, like SQL Server, XML documents, Objects in memory, etc.
You don’t need to know the syntax specific to data source because LINQ enables us to work with the different data source.
If we develop one application and this working with databases, XML documents and/or in memory objects then we can use LINQ.
LINQ query can be using any .NET supported programming language because of LINQ component between the LINQ query and the data source.
LinqPad tool can be downloaded for free from here. Now I help you to learn, write and test LINQ.
Today I will try to use C# with LinqPad.
LinqPad helps you to under C# to make it converted to Intermediate Language (IL) which is also known as Microsoft Intermediate Language or Common Intermediate Language.
If you try to use LinqPad without source code then the only error under C# will come into C# Statement expected
Press F4 button then press Add button (Query Properties ) and type WindowsBase.dll (Add Custom Assembly References – window) then press the Ok button.
Let’s test all. Wrote this example and under Results tab click under IL.
Example 1
C# Expression
1 | DateTime.Now.ToString("dd-MMM-yyyy") |
Result IL
1 2 3 4 5 6 7 8 | IL_0000: call System.DateTime.get_Now IL_0005: stloc.0 // CS$0$0000 IL_0006: ldloca.s 00 // CS$0$0000 IL_0008: ldstr "dd-MMM-yyyy" IL_000D: call System.DateTime.ToString IL_0012: call LINQPad.Extensions.Dump IL_0017: pop IL_0018: ret |
Example 2
C# Statement
1 2 3 4 5 | int[] numbers = {1,2,3,4,5}; var result = from n in numbers where n > 0 select n; result.Dump(); |
Result IL:
1 2 3 4 5 | IL_0018: brtrue.s IL_002B IL_001A: ldnull IL_001B: ldftn b__0 IL_0021: newobj System.Func<System.Int32,System.Boolean> ... |
this can a little dizzy for you but will come with future details.
Example 3
C# Program
1 2 3 4 5 6 7 8 | static void IterationExample() { int i = 0; while (i < 5) { i++; } } |
Results IL :
1 2 3 4 5 6 7 8 9 10 11 12 | IterationExample: IL_0000: ldc.i4.0 IL_0001: stloc.0 // i IL_0002: br.s IL_0008 IL_0004: ldloc.0 // i IL_0005: ldc.i4.1 IL_0006: add IL_0007: stloc.0 // i IL_0008: ldloc.0 // i IL_0009: ldc.i4.5 IL_000A: blt.s IL_0004 IL_000C: ret |
This example will show us the results over IL.
Using LinqPad with assemblies .NET.
Under C# the most simple DLL example can be:
1 2 3 4 5 6 7 8 9 10 | using System; public class Test { public static void Test() { Console.WriteLine("tutorial LinqPad -001"); } } |
If we wrote into LinqPad under C# Statement and then run it.
Console.WriteLine(“tutorial LinqPad -001”);
… the result will be :
1 2 3 | IL_0000: ldstr "tutorial LinqPad -001" IL_0005: call System.Console.WriteLine IL_000A: ret |
The next step is to create one new file into our C# project named Test.dll with this source code:
1 2 3 4 5 6 7 8 9 10 | .assembly Test {} .assembly extern mscorlib {} .class public Test extends [mscorlib]System.Object { .method public static void TestMethod() cil managed { ldstr "tutorial LinqPad -001" call void [mscorlib]System.Console::WriteLine(string) ret } } |
If you parse this code you will see under call I need to add this :
1 2 3 4 5 6 7 | .assembly Test {} .assembly extern mscorlib {} .class public Test extends [mscorlib]System.Object { .method public static void TestMethod() cil managed { void [mscorlib] :: (string) } } |
This is just one simple example of LinqPad.This software’s very good when working with SQL, ASP, data, arrays.
To make the DLL file you need this: ilasm.exe
Run the command under cmd.exe:
1 | C:\C#>C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ilasm.exe /DLL /OUT:Test.dll Test.il |
The result will be Test.dll.
Let test it:
Make another file with notepad, named DllTest.cs and put this source code:
1 2 3 4 5 6 7 8 | using System; class MainClass { static void Main() { Test.TestMethod(); } } |
Use cmd.exe to compile this into EXE file format :
1 | C:\C#>C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe /r:Test.dll DllTest.cs |
If you run this using cmd.exe the output will be:
1 2 | C:\C#>DllTest.exe tutorial LinkPad -001 |