-
Notifications
You must be signed in to change notification settings - Fork 113
Data.Linq.QuickStart
Use this demo project to play with BLToolkit Linq provider.
The following example just takes the Employee table and iterates through it.
static void FirstTest()
{
using (var db = new NorthwindDB())
{
var query = db.Employee;
foreach (var employee in query)
{
Console.WriteLine("{0} {1}", employee.EmployeeID, employee.FirstName);
}
}
}
Simple, isn't it? However let's take a closer look. Put breakpoint inside the loop and inspect the query and db variables in the debugger.
Open the Text Visualizer to inspect the SqlText property of the query variable:
This is an SQL query generated by BLToolkit. The first line contains commented current configuration string, data provider name, and SQL provider name.
Unfortunately, the SqlText property is available if we have a variable of the IQueriable type as shown in the example above. It's not always possible. The following query returns a value of type int.
static void CountTest()
{
using (var db = new NorthwindDB())
{
int count = db.Employee.Count();
Console.WriteLine(count);
}
}
In this case we can make use of the db.LastQuery property that represents the latest database query SQL text.
Another way to retrieve the same information without inspecting variables is to switch on the BLToolkit debug tracing mode:
static void Main()
{
DbManager.TurnTraceSwitchOn();
FirstTest();
CountTest();
}
This mode is available in the debug version of BLToolkit.
Now let's query something more interesting:
static void SingleTableTest()
{
using (var db = new NorthwindDB())
{
var query =
from e in db.Employee
where e.EmployeeID > 5
orderby e.LastName, e.FirstName
select e;
foreach (var employee in query)
{
Console.WriteLine("{0} {1}, {2}", employee.EmployeeID, employee.LastName, employee.FirstName);
}
}
}
Generated SQL:
SELECT
[e].[EmployeeID],
[e].[LastName],
[e].[FirstName],
[e].[Title],
[e].[TitleOfCourtesy],
[e].[BirthDate],
[e].[HireDate],
[e].[Address],
[e].[City],
[e].[Region],
[e].[PostalCode],
[e].[Country],
[e].[HomePhone],
[e].[Extension],
[e].[Notes],
[e].[ReportsTo],
[e].[PhotoPath]
FROM
[Employees] [e]
WHERE
[e].[EmployeeID] > 5
ORDER BY
[e].[LastName],
[e].[FirstName]