1. 简单查询

                var result = from c in db.Customers select c;

2. 条件查询

   普通 LINQ 写法:

                var result = from c in db.Customers where c.Country == "UK" select c;

   Lambda 表达式写法:

                var result = db.Customers.Where(c => c.Country == "UK");

3. 排序分页

                IQueryable<Customers> cus10 = (from c in result orderby c.CustomerID select c).Skip(0).Take(10);

4. 聚合

            using (var edm = new NorthwindEntities())
            {
                var maxuprice = edm.Products.Max(p => p.UnitPrice);
                Console.WriteLine(maxuprice.Value);
            }
5. 连接

    可以使用的连接有 Join 和 GroupJoin 方法。

    GroupJoin 组连接等效于左外连接,返回第一个(左侧)数据源的每个元素(即使其他数据源中没有关联元素)。

            using (var edm = new NorthwindEntities())
            {
                var query = from d in edm.Order_Details
                            join order in edm.Orders                //重点
                            on d.OrderID equals order.OrderID       //重点
                            select new              //这也是重点
                            {
                                OrderId = order.OrderID,
                                ProductId = d.ProductID,
                                UnitPrice = d.UnitPrice
                            };

                foreach(var q in  query)
                {
                    Console.WriteLine("{0},{1},{2}", q.OrderId, q.ProductId, q.UnitPrice);
                }
            }

    EF 不支持复杂类型(如实体)的直接检索,只能用简单类型,比如常用的标量类型 string、int 和 guid。(即说的是可以是 c == "",但不可以是 c == 某个实体类对象)

    如果出现此错误,会引发 NotSupportedException 异常,并显示消息 “无法创建 ‘System.Object’ 类型的常量值”。

6. 分组查询
            using (var db = new NorthwindEntities())
            {
                var query = from c in db.Categories
                            join p in db.Products
                            on c.CategoryID equals p.CategoryID
                            group new { c, p } by new { c.CategoryName } into g
                            select new
                            {
                                g.Key.CategoryName,
                                SumPrice = (decimal?)g.Sum(pt => pt.p.UnitPrice),
                                Count = g.Select(x => x.c.CategoryID).Distinct().Count()
                            };
            }