设为首页 加入收藏

TOP

BLToolkitLinq-provider(四)
2015-11-21 01:53:40 来源: 作者: 【 】 浏览:8
Tags:BLToolkitLinq-provider
us to 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 vospolzovatesya property db.LastQuery, which contains the text of the last of a database query.

And the last way to get all the same information, but without looking at the variables of objects - is to enable tracing DbManager.

static void Main()
{
    DbManager.TraceSwitch = new TraceSwitch("DbManager", "DbManager trace switch", "Info");

    FirstTest();
    CountTest();
}

Naturally, this mode is only available when using the debug version BLToolkit.

The following example, we need more for the order to smoothly move on to more complex queries.

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);
        }
    }
}

NOTE

Next article provides methods will not completely, but only in part related to the demonstration.

Generated SQL:

SELECT
    [And]. [EmployeeID]
    [e].[LastName],
    [e].[FirstName],
    [e].[Title],
    [And]. [TitleOfCourtesy]
    [e].[BirthDate],
    [And]. [HireDate]
    [e].[Address],
    [e].[City],
    [e].[Region],
    [And]. [PostalCode]
    [e].[Country],
    [E]. [HomePhone],
    [e].[Extension],
    [e].[Notes],
    [And]. [ReportsTo]
    [And]. [PhotoPath]
FROM
    [Employees] [e]
WHERE
    [e].[EmployeeID] > 5
ORDER BY
    [e].[LastName],
    [e].[FirstName]

Query two tables

The following are standard requests to the two (or more) tables. They will be needed in the future to discuss other possibilities BLToolkit.

Select Many

"Old style 'Join.

from c in db.Category
from p in db.Product
where p.CategoryID == c.CategoryID
select new
{
    c.CategoryName,
    p.ProductName
???

SQL:

SELECT
    [c].[CategoryName],
    [T1]. [ProductName]
FROM
    [Categories] [c], [Products] [t1]
WHERE
    [t1].[CategoryID] = [c].[CategoryID]

Inner Join

"New" Join.

from p in db.Product
join c in db.Category on p.CategoryID equals c.CategoryID
select new
{
    c.CategoryName,
    p.ProductName
};
 
SQL:
SELECT
    [t1].[CategoryName],
    [P]. [ProductName]
FROM
    [Products] [p]
        INNER JOIN [Categories] [t1] ON [p].[CategoryID] = [t1].[CategoryID]

Left Join

And finally, here is a plain Left Join.

from p in db.Product
join c in db.Category on p.CategoryID equals c.CategoryID into g
from c in g.DefaultIfEmpty()
select new
{
    c.CategoryName,
    p.ProductName
}

SQL:

SELECT
    [t1].[CategoryName],
    [P]. [ProductName]
FROM
    [Products] [p]
        LEFT JOIN [Categories] [t1] ON [p].[CategoryID] = [t1].[CategoryID]

Implementation of the standard features

Association

Associations are called relationships between entities. Association are set by the attributes and greatly simplify queries due to the fact that every time eliminates the need to specify the relationship between binding tables.

Below is a table Product and its association.

[TableName("Products")]
public abstract class Product
{
    [PrimaryKey, Identity] public int      ProductID;
    [NotNull]               public  string    ProductName;
                            public  int ? SupplierID;
                            public  int ? C
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/13/13
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Mysql Master Slave Config 下一篇BI-SSAS简介篇

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: