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(); } }