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();  
  • 相关阅读:
    Django ORM查询总结
    Django中settings.py 相关配置说明
    项目完整开发流程
    生产环境中不小心升级了glibc该怎么办?
    提取网页里所有链接的方法
    fofa基本语法
    python爬取豆瓣top250电影源码
    一键清理系统的缓存
    内网隧道搭建---Neo-reGeorg
    若依后台管理系统sql注入和未授权访问
  • 原文地址:https://www.cnblogs.com/liuyu7177/p/3105688.html
Copyright © 2011-2022 走看看