zoukankan      html  css  js  c++  java
  • Linq 条件查询的一些方法

    1.LINQ的IN:
    var queryResult = from p in db.Products
    where (new int?[] {1,2}).Contains(p.CategoryID)
    select p;

    2.LINQ的NOT IN:
    var queryResult = from p in db.Products
    where !(new int?[] { 1, 2 }).Contains(p.CategoryID)
    select p;

    3.LINQ的NOT IN 遍历集合

    List<string> source = new List<string>{ "aaa", "bbb" };        

    List<string> px = new  List<string> { "aaa", "bbb", "ccc" }; 

     var q = from s in source   

            from p in px                  

             where s==p        

              select s;

    4.SQL中的in和not in语法对应的LINQ写法

        List<testInfo> listAll = new List<testInfo>();  
        listAll.Add(new testInfo() { id = 1, name = "11", test = "111" });  
        listAll.Add(new testInfo() { id = 2, name = "22", test = "222" });  
        listAll.Add(new testInfo() { id = 3, name = "33", test = "333" });  
        listAll.Add(new testInfo() { id = 4, name = "33", test = "333" });  
          
        List<testInfo> listFind = new List<testInfo>();  
        listFind.Add(new testInfo() { id = 1, name = "44", test = "111" });  
        listFind.Add(new testInfo() { id = 2, name = "22", test = "222" });  
        listFind.Add(new testInfo() { id = 3, name = "33", test = "333" });  
        listFind.Add(new testInfo() { id = 4, name = "55", test = "333" });  
        //SQL中的in和not in语法对应的LINQ写法  
       //相当于查询语句:select * from listAll p where p.name not in(select f.name from listFind)  
        var a = (from p in listAll  
                 where !(from f in listFind select f.name).Contains(p.name)  
                 select p).ToList();  
  • 相关阅读:
    (转)CentOS 和 Ubuntu 下的网络配置
    love 的Python 表示
    python mysqlLdb ImportError: DLL load failed: 找不到指定的模块
    elasticsearch7.11.1安装及使用小记
    python多进程代码示例
    在c++项目中使用高性能的rapidjson作为json处理库
    使用kenlm进行文本纠错
    供應商主檔建立流程
    SAP系統自帶范例
    内部订单作业流程
  • 原文地址:https://www.cnblogs.com/liuyu7177/p/3105688.html
Copyright © 2011-2022 走看看