• EF数据Linq方式查询


                using (var ctx = new NorthwindEntities())
                {
                    //单表查询SQL查询方式
                    //SELECT * FROM Customers AS c WHERE c.City = 'London' AND c.CustomerID = 'EASTC'
                    dataGridView1.DataSource = (from c in ctx.Customers where c.City == "London" && c.CustomerID == "EASTC" select c).ToList();
    
                    //模糊查询
                    //SELECT * FROM Customers AS c WHERE c.City LIKE 'L%'
                    dataGridView1.DataSource = (from c in ctx.Customers where c.City.StartsWith("L") select c).ToList();
                    //SELECT * FROM Customers AS c WHERE c.City LIKE '%L'
                    dataGridView1.DataSource = (from c in ctx.Customers where c.City.EndsWith("L") select c).ToList();
                    //SELECT * FROM Customers AS c WHERE c.City LIKE '%L%'
                    dataGridView1.DataSource = (from c in ctx.Customers where c.City.Contains("L") select c).ToList();
                    //SELECT * FROM Customers AS c WHERE c.City='London' ORDER BY c.CustomerID
    
                    //排序
                    //SELECT * FROM Customers AS c WHERE c.City='London' ORDER BY c.CustomerID
                    dataGridView1.DataSource = (from c in ctx.Customers where c.City == "London" orderby c.CustomerID select c).ToList();
    
                    //只查询部分列,并排序
                    //SELECT c.CustomerID, c.CompanyName, c.ContactName FROM Customers AS c
                    //WHERE c.CompanyName = 'The Big Cheese'
                    //ORDER BY c.CompanyName
                    dataGridView1.DataSource = (from c in ctx.Customers where c.CompanyName == "The Big Cheese" orderby c.CompanyName select new { c.CustomerID, c.CompanyName, c.ContactName }).ToList();
    
                    //分组
                    //SELECT c.City,COUNT(c.CompanyName) FROM Customers AS c GROUP BY c.City
                    dataGridView1.DataSource = (from c in ctx.Customers group c by c.City into r orderby r.Count() descending select new { City = r.Key, Count = r.Count() }).ToList();
                }
            }

    数据模型

    界面

  • 相关阅读:
    ajax请求超时
    tp5去重统计某字段的数量
    html本地存储 localStorge
    json、obj转换
    关于数组的合并arr.push() arr.push.apply()
    curl内容
    js 回车键登录
    tp5 前置操作
    STL容器
    c++文件的读写
  • 原文地址:https://www.cnblogs.com/liessay/p/11911059.html
Copyright © 2020-2023  润新知