zoukankan      html  css  js  c++  java
  • 6,探究特性

    概要:

           学习Linq语句关于类似缓存,延迟,限制,隔离等的使用。

      个人学习笔记,仅供个人复习,学习来源在第一篇中有链接。

    内容:

    1, 延缓执行IQueryable

    IQueryable query = from c inctx.Customers select c;

    这样的查询句法不会导致语句立即执行,它仅仅是一个描述,对应一个SQL。仅仅在需要使用的时候才会执行语句.

    如果你执行两次foreach操作,将会捕获到两次SQL语句的执行:

    IQueryable query = from c inctx.Customers select c;

                               foreach (Customer c inquery)

                               Response.Write(c.CustomerID);

                       foreach (Customerc in query)

                              Response.Write(c.ContactName);

             对于这样的需求,建议你先使用ToList()等方法把查询结果先进行保存,再对集合进行查询:

                        IEnumerable<Customer>customers = (from c inctx.Customers select c).ToList();

                    foreach(Customer c incustomers)

                      Response.Write(c.CustomerID);

                               foreach (Customer c incustomers)

                     Response.Write(c.ContactName);

           2,DataLoadOptions

                  DataLoadOptions options = new DataLoadOptions();

    options.LoadWith<Product>(p=> p.Order_Details);

    options.AssociateWith<Product>(p=> p.Order_Details.Where(od => od.Quantity > 80));

    ctx.LoadOptions = options;

    var products = from p in ctx.Products select p;

                  Linqto sql对DataLoadOptions的使用是有限制的,它只支持1个1对多的关系。

                  DataLoadOptions options = new DataLoadOptions();

                               options.LoadWith<Customer>(c =>c.Orders);

                               options.LoadWith<Order>(o =>o.Order_Details);

                       ctx.LoadOptions= options;

                              IEnumerable<Customer>customers = ctx.Customers.ToList<Customer>();

                       而对于多对1的关系,Linq to sql对于DataLoadOptions没有限制:

                  DataLoadOptions options = new DataLoadOptions();

                               options.LoadWith<Product>(c =>c.Category);

                               options.LoadWith<Product>(c => c.Order_Details);

                               options.LoadWith<Order_Detail>(o =>o.Order);

                               ctx.LoadOptions = options;

                              IEnumerable<Product>products = ctx.Products.ToList<Product>();

        3,主键缓存

           Linq to sql对查询过的对象进行缓存,之后的如果只根据主键查询一条记录的话会

    直接从缓存中读取:

                               Customer c1 =ctx.Customers.Single(customer => customer.CustomerID == "ANATR");

                               c1.ContactName = "zhuye";

                               Customer c2 =ctx.Customers.Single(customer => customer.CustomerID == "ANATR");

                              Response.Write(c2.ContactName);

        4,DataContext隔离

                  有的时候我们会把对象从外部传入DataContext,要求它更新,由于不同的DataContext是相对独立的。由于新的DataContext中还没有获取实体,我们只能通过附加方式更新数据。

           首先把Customer表的主键字段加上IsVersion标识:

        [Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL", CanBeNull=false, IsPrimaryKey=true, IsVersion = true)]

           运行下面的测试代码:

            Customer c = new Customer { CustomerID = "ALFKI", ContactName = "zhuye", CompanyName = "1111" };

            ctx.Customers.Attach(c, true);

            ctx.SubmitChanges();

  • 相关阅读:
    生成器笔记
    迭代器笔记
    hashilib_module
    Ubuntu操作及各种命令笔记
    python正则表达式2
    python正则表达式1
    python文件操作(with关键字)
    python文件操作
    python包
    python模块
  • 原文地址:https://www.cnblogs.com/yaoge/p/1816728.html
Copyright © 2011-2022 走看看