zoukankan      html  css  js  c++  java
  • LinqER

    1. 查询语法:
    运算符: var data from c in db.Customers where c.Countr=“Spain” select c;
         var data=db.Customers.Where(c=> c.Country==“Spain”);
    •Select运算符用于将数据源中的内容投影到内存中,用于指定选取对象中的哪些内容。还可以细化:用where和orderby对返回的对象分别进行筛选和排序。
    •From运算符用于绑定数据集合。
    •Where 运算符接收bool表达式: Not、And、Or、==
    •Orderby: Descending、asc
    •New{}用于提示底层系统生成的对象只带有列出的属性。是一个匿名类型,所有属性被赋予一个默认名称,
    •Join on / Take /Skip/First/Single
     
    2. 
    •Linq-to-DataSet:
    var customers=ds.Tables[0].AsEnumerable();
    var orders=ds.Tables[1].AsEnumerable();
    var data=from o in orders join c in customers ono.Field<string>(“CustomerId”) equals c.Field<string>(“CustomerId”)
      where o.Field<DateTime>(“OrderDate”).Year==1989  select new (OrderId=o.Field<int>(“OrderId”),Company=c.Field<string>(“CompanyName”))
     
    •Linq-to-Object: 允许对任何实现了IEnumerable和IQueryable的类型执行复杂的查询。
    Int[ ] arrs=new int[ ]{1,2,3,4,5,6,7};
    Var data =from n in arrs where n%2==0 select n; 返回偶数集合。
    还可以对结果集进行计算: data.Sum()、Average、Max、Min 等 
     
    •Linq-to-SQL:  var dat=from c in dataContext.Customers
    where c.County==“Span” Select c;
    From 从逻辑上对指定集合的内容进行遍历;
    Where 用于对集合的每一个对象进行计算
    Select 将当前满足条件的对象添加到要返回的列表中。
     
    •Linq-to-Xml: var data=XDocument.Load(“Books.xml”);
    var selectedBooks=from b in data.Book where b.Author==“Dino” select b.Title;
     
    3.Linq的内部机制
     
    •延迟查询:
    var data=from c in dataContext.Customers where c.Country=“Spain” select c;执行完这句话,data中并没有数据,数据库也没有执行sql查询; 直到遇到用于获取查询结果的代码才会触发查询语句。
    •预获取:我们可以通过 遍历data、通过数据绑定或者使用ToList、ToArray方法来及早的将数据加载到内存。
     
    4. 与SQLServer的交互:
     Linq-to-SQL 数据模型、O/R设计器
    数据上下文类继承自System.Data.Linq.DataContext;
    扩展性方法主要目的是:使开发者能够在该对象生命周期的特定时刻执行自己编写的代码
    增删改查demo
     
    >1.创建上下文 MyDataContext dataContext=new MyDataContext();
    预加载数据:
    DataLoadOptions options=new DataLoadOptions();
    options.LoadWith<Customer>(c=>c.Orders);
    dataContext.LoadOptions=options;
    >2.更改数据
    string id =“admin”;
    Customer c=dataContext.Customers.Single(c=>c.CustomerId==id);
    c.Address=“china”;
    dataContext.SubmitChanges();
    >3.添加数据:
    Customer c=new Customer();
    c.CustomerId=“123”; c.Address=“china”;
    dataContext.Customers.InsertOnSubmit(c);
    dataContext.SubmitChanges();
    >4.删除:
    Customer c=dataContext.Customer.SingleOrDefault(c=>c.CustomerId=“123”);
    if (c!=null)
      dataContext.Customer.DeleteOnSubmit();
    dataContext.SubmitChanges();
     >5. 当然也可以使用事务、存储过程。不在演示;
  • 相关阅读:
    15、常量指针和指针常量区别?
    14、strlen和sizeof区别?
    12、变量声明和定义区别?
    10、宏定义和函数和typedef有何区别?
    hdoj--1495--非常可乐(搜索+隐式图)
    hdoj--2579--Dating with girls(2)(搜索+三维标记)
    poj--3630--Phone List(字典树+前缀判断)
    poj--2001--Shortest Prefixes(字典树)
    Huatuo's Medicine
    hdoj--2803--The MAX(水题)
  • 原文地址:https://www.cnblogs.com/qlbk/p/2997463.html
Copyright © 2011-2022 走看看