您的位置:知识库 » 数据库

一步一步学Linq to sql(九):其它补充

作者: lovecherry  来源: 博客园  发布时间: 2008-09-26 23:16  阅读: 7326 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

一步一步学Linq to sql(一):预备知识

一步一步学Linq to sql(二):DataContext与实体

一步一步学Linq to sql(三):增删改

一步一步学Linq to sql(四):查询句法

一步一步学Linq to sql(五):存储过程

一步一步学Linq to sql(六):探究特性

一步一步学Linq to sql(七):并发与事务

一步一步学Linq to sql(八):继承与关系

一步一步学Linq to sql(九):其它补充

一步一步学Linq to sql(十):分层构架的例子


已编译查询

       对于一些在项目中经常被用到的查询可以封装成已编译查询,这样就能提高执行效率:

static class Queries

{

    public static Func<NorthwindDataContext, string, IQueryable<Customer>>

        CustomersByCity = CompiledQuery.Compile((NorthwindDataContext ctx, string city) => from c in ctx.Customers where c.City == city select c);

}

       调用查询方式如下:   

        GridView1.DataSource = Queries.CustomersByCity(ctx, "London");

        GridView1.DataBind();

 
获取一些信息

        var query = from c in ctx.Customers select c;

        Response.Write("Provider类型:" + ctx.Mapping.ProviderType + "<br/>");

        Response.Write("数据库:" + ctx.Mapping.DatabaseName + "<br/>");

        Response.Write("表:" + ctx.Mapping.GetTable(typeof(Customer)).TableName + "<br/>");

        Response.Write("表达式:" + query.Expression.ToString() + "<br/>");

        Response.Write("sql" + query.Provider.ToString() + "<br/>");

       上面的代码执行结果如下:

Provider类型:System.Data.Linq.SqlClient.SqlProvider
数据库:Northwind
表:dbo.Customers
表达式:Table(Customer).Select(c => c)
sql
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0]窗体顶端

窗体底端

0
0
 
标签:Linq sql 基础

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻