ASP.NET LINQ to SQL 八大字句

八大字句:

From …in子句:执行查询操作的数据源和范围变量

var q =
    from c in db.Customers
    from o in c.Orders
    where c.City == "London"
    select o;

Select子句:指定查询结果的类型和表现形式

var q =
    from c in db.Customers
    select c.ContactName;

Where 子句:筛选元素的逻辑条件,一般由逻辑运算符组成

var q =
    from c in db.Customers
    where c.City == "London"
    select c;

例如:使用where筛选在伦敦的客户

var q =
    from c in db.Customers
    where c.City == "London"
    select c;

Group…by子句:对查询进行分组

var q =
    from p in db.Products
    group p by p.CategoryID into g
    select g;

Orderby子句:对查询结果进行排序,可以分为降序、升序。

var q =
    from c in db.Customers
    orderby c.City, c.ContactName
    select c;

Join子句:连接多个查询操作的数据源

var q =
    from e in db.Employees
    join o in db.Orders on e equals o.Employee into ords
    from o in ords.DefaultIfEmpty()
    select new
    {
        e.FirstName,
        e.LastName,
        Order = o
    };

Let子句:引入用于存储查询表达式中的子表达式结果的范围变量

var query =
                from sentence in strings
                let words = sentence.Split(' ')//用空格分割成数组
                from word in words
                let w = word.ToLower()//把每个字母小写
                where w[0] == 'a' || w[0] == 'e'
                select word;

Into子句:提供一个临时标识符,充当对join、group或select子句的结果

 var byHow = from how in  vehicles

                        join trans in transports

                        on how.vehicleHow equals trans.How

                        into lst                                           //    此lst是IEnumerable<transport> 类型

                        select new { How=how, Tlist = lst };


猜你喜欢

转载自blog.csdn.net/qq_41851370/article/details/79841881
今日推荐