Linq语法

1.一对多关系查询:
var q=from c in db.Students
      from n in c.orders
      where c.City=='wuhan'
      select  n;


2.多对多关系查询:
 var q=from e in db.Students
       from et in e.name
       where e.city=='wuhan'
       select new 
          {
             e.firstName,
             e.lastName
           }


3.where 简单形式查询
  var q=from c in db.students
     where c.city=="hhh"
     select c;




条件筛选:
 var q=from c in db.students
     where c.sex>=5
     select c;


4.first(实质就是sql中top1)
简单用法:选择表中的第一个发货方。
Shipper shipper = db.Shippers.First();


选择CustomerID 为“BONAP”的单个客户
Customer cust = db.Customers.First(c => c.CustomerID == "BONAP");




5.匿名函数:
  var q =
    from c in db.Customers
    select new {c.ContactName, c.Phone};
  使用 SELECT 和匿名类型返回仅含客户联系人姓名和电话号码的序列;




6.where 和order by形式:
  var q =
    from o in db.Orders
    where o.ShipCity == "London"
    orderby o.Freight
    select o;


7.Concat(连接)简单形式:
 返回所有消费者和雇员的电话和传真
  var q = (
         from c in db.Customers
         select c.Phone
        ).Concat(
         from c in db.Customers
         select c.Fax
        ).Concat(
         from e in db.Employees
         select e.HomePhone
        );


 Union(合并)


8.插入(Insert)::::
NorthwindDataContext db = new NorthwindDataContext();
var newCustomer = new Customer
{
    CustomerID = "MCSFT",
    CompanyName = "Microsoft",
    ContactName = "John Doe",
    ContactTitle = "Sales Manager",
    Address = "1 Microsoft Way",
    City = "Redmond",
    Region = "WA",
    PostalCode = "98052",
    Country = "USA",
    Phone = "(425) 555-1234",
    Fax = null
};
db.Customers.InsertOnSubmit(newCustomer);
db.SubmitChanges();


9.更新update::::
  Customer cust =db.Customers.First(c => c.CustomerID == "ALFKI");
    cust.ContactTitle = "Vice President";
    db.SubmitChanges();


10.删除delete::::::
   OrderDetail orderDetail =
    db.OrderDetails.First(c => c.OrderID == 10255 && c.ProductID == 36);
    db.OrderDetails.DeleteOnSubmit(orderDetail);
    db.SubmitChanges();
  

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014424282/article/details/72871129
文章标签: Linq
个人分类: Linq
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭