zoukankan      html  css  js  c++  java
  • EF数据Linq方式查询

    using (var ctx = new AppDbContext())
                {
                    //单表查询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();
                }
            }
  • 相关阅读:
    NYOJ 205
    NYOJ 187
    NYOJ 105
    NUOJ 88
    NYOJ 70
    LL(1)算法
    MATLAB的一些基础知识
    Ubuntu raid5+lvm实验
    空间滤波
    认识weblogic的各个机构
  • 原文地址:https://www.cnblogs.com/wfy680/p/15114079.html
Copyright © 2011-2022 走看看