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();  
  • 相关阅读:
    Ansible快速实战指南----多机自动化执行命令、部署神器
    linux 挂载共享盘
    蓄水池抽样算法
    PCA MATLAB代码
    网口转串口
    通过Python收集MySQL MHA 部署及运行状态信息的功能实现
    Linux常用命令总结(二)
    python 学习笔记 (四)
    MySQL 学习笔记(四)
    学习ProxySQL参考到几个网址
  • 原文地址:https://www.cnblogs.com/liuyu7177/p/3105688.html
Copyright © 2011-2022 走看看