zoukankan      html  css  js  c++  java
  • 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();
                }
            }

    数据模型

    界面

  • 相关阅读:
    eclipse workspace
    logcat and monkey
    git command
    Take Total Control of Internet Explorer with Advanced Hosting Interfaces
    #import 指令
    今天聊发兴致,写了一个 COM STEP BY STEP,结果。。。
    DECLARE_CLASSFACTORY_SINGLETON 宏
    对象微操
    宏定义缺失的解决
    读取地址本内容
  • 原文地址:https://www.cnblogs.com/liessay/p/11911059.html
Copyright © 2011-2022 走看看