您的位置:首页 > 编程学习 > ASP.NET > 正文

linq not in 查询

更多 时间:2015-11-4 类别:编程学习 浏览量:1394

linq not in 查询

linq not in 查询

下面以linq to sql的实例介绍 not in 查询和 in查询,linq的用法类似

 

一、T-SQL的IN

 

  • 
    Select ProductID, ProductName, CategoryID
    From dbo.Products
    Where CategoryID in (1, 2)
    
    		
  •  

     

    二、LINQ to SQL 实现IN

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

  •  

    三、T-SQL的NOT IN

  •  
  • 
    Select ProductID, ProductName, CategoryID
    From dbo.Products
    Where CategoryID not in (1, 2)
    
    				
  •  

    四、LINQ to SQL 实现 NOT IN

  •  
  • 
    var queryResult = from p in db.Products
    where !(new int[] {1,2}).Contains(p.CategoryID)
    select p;
    
    		
  •  

    五、LINQ to SQL 实现 NOT IN子查询

  •  
  • 
    from a in TableA where !(from b in TableB Where ... select b.Id).Contains(a.Id)
    
    		
  •  

    六、Linq 实现方式

  •  
  • 
    //比如 Id in (1,2,3)
    
    int[] a={1,2,3};
    list.Where(x=>a.Contains(x.Id)) 
    
    				
  •  

    标签:linq