• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

我们将共同携手迎接你的到来!


新的一年
新的开始
新的目标
新的收获

kevin 愛戀 20140103

博客园          联系   管理     
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]

posted on 2010-06-01 13:01  kevin_20131022  阅读(165)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3