Linq 中的 in 与 not in 的使用
接 触 LINQ 也有很长的一段时间了,有些在 SQL 语句中用的很顺手的东西在 Linq 中却不知道如何实现了,最近遇到了一个问题,在 LINQ 的 Where 条件式中要如何使用 IN 与 NOT IN 呢? 这时候真的开始怀念 T-SQL 其实还是最好用的。为了让自己日后开发时更为方便,于是花了一点时间,参考一些网络资料及 MSDN 后,得到以下的测试結果:
T-SQL的IN:
Select ProductID, ProductName, CategoryIDFrom dbo.ProductsWhere CategoryID in (1, 2)
T-SQL的NOT IN:
Select ProductID, ProductName, CategoryIDFrom dbo.ProductsWhere CategoryID not in (1, 2)
Or
Select ProductID, ProductName, CategoryIDFrom dbo.ProductsWhere not CategoryID in (1, 2)
LINQ的IN:
var queryResult = from p in db.Productswhere (new int?[] {1,2}).Contains(p.CategoryID)select p;
LINQ的IN解析成SQL:
SELECT
[t0].[ProductID], [t0].[ProductName], [t0].[SupplierID],
[t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice],
[t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel],
[t0].[Discontinued] FROM [dbo].[Products]AS [t0]WHERE [t0].[CategoryID] IN (@p0, @p1)
LINQ的NOT IN:
var queryResult = from p in db.Productswhere ! (new int?[] {1,2}).Contains(p.CategoryID)select p;
LINQ的NOT IN解析成SQL:
SELECT
[t0].[ProductID], [t0].[ProductName], [t0].[SupplierID],
[t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice],
[t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel],
[t0].[Discontinued] FROM [dbo].[Products]AS [t0]WHERE NOT [t0].[CategoryID] IN (@p0, @p1)
实例:
var topicListid = (from t in db.TopicInCategory
where t.TopicCategoryInfoID == id
select t.TopicInfoID).ToList(); var topicinfos = from t in db.TopicInfo
where !(topicListid).Contains(t.TopicInfoID)
select t;
Linq 中的 in 与 not in 的使用的更多相关文章
- Entity Framework 6 Recipes 2nd Edition(11-9)译 -> 在LINQ中使用规范函数
11-9. 在LINQ中使用规范函数 问题 想在一个LINQ查询中使用规范函数 解决方案 假设我们已经有一个影片租赁(MovieRental )实体,它保存某个影片什么时候租出及还回来,以及滞纳金等, ...
- Entity Framework 6 Recipes 2nd Edition(11-11)译 -> 在LINQ中调用数据库函数
11-11. 在LINQ中调用数据库函数 问题 相要在一个LINQ 查询中调用数据库函数. 解决方案 假设有一个任命(Appointment )实体模型,如Figure 11-11.所示, 我们想要查 ...
- 关于Linq中的Lambda表达式中OrderBy的深入理解
起因:就是一段Linq语句,OrderBy里面的i是什么? IQueryable<Student> slist = (from s in EFDB.Student select s). O ...
- Linq中关键字的作用及用法
Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...
- Linq 中按照多个值进行分组(GroupBy)
Linq 中按照多个值进行分组(GroupBy) .GroupBy(x => new { x.Age, x.Sex }) group emp by new { emp.Age, emp.Sex ...
- Linq 中的 left join
Linq 中的 left join 表A User: 表B UserType: Linq: from t in UserType join u in User on t.typeId equal u. ...
- LINQ中的一些查询语句格式
LINQ的基本格式如下所示:var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式> ...
- Linq 中查询一个表中指定的字段
//Linq中查询一个表中指定的几个字段: ); // FindAllItems()为查询对应表的所有数据的方法: // Where 里面为查询条件 // Select 为查询的筛选条件 new{} ...
- linq中AsEnumerable和AsQueryable的区别
本文导读:用Linq来操作集合的时候会用到AsQueryable()和AsEnumerable(),何时该用AsQueryable()和何时该用AsEnumerable(),或许存在些疑惑.AsQue ...
- Linq中使用Left Join
use Test Create table Student( ID ,) primary key, ) not null ) Create Table Book( ID ,) primary key, ...
随机推荐
- @classmethod 与 @staticmethod 区别
- codeforces472B
Design Tutorial: Learn from Life CodeForces - 472B One way to create a task is to learn from life. Y ...
- git如何拉取指定分支的代码
问题背景: 新项目还在开发阶段,没有正式对外发布,所以开发同事合并代码到develop上(或者其他名称分支上),而不是到master分支上 通过git拉取代码的时候,默认拉取的是master分支,如下 ...
- 构建squid代理服务器
基本概念 本文使用squid代理服务 软件介绍:百度百科 作为应用层的代理服务软件,Squid主要提供缓存加速.应用层过滤控制的功能: 工作机制:缓存网页对象,减少重复请求(HTTP代理的缓存加速对象 ...
- Git如何合并分支代码
如果是在IDEA中使用Git,可以直接合并代码. 我当前的开发分支,是feature/bing,现在我需要合并分支 feature/xxs 上的代码,这个过程只需要在IDEA中完成. 1.更新代码,会 ...
- 【 HDU 4936 】Rainbow Island (hash + 高斯消元)
BUPT2017 wintertraining(15) #5B HDU - 4936 2014 Multi-University Training Contest 7 F 题意 直接看官方的题意和题解 ...
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
- luogu4267 TamingtheHerd (dp)
设f[i][j]为让前i天发生j次暴动需要改变的最少的值 则f[i][j]=min{f[k][j-1]+(x[k+1]!=0)+(x[k+2]!=1)+...+(x[i]!=(i-k-1))} $O( ...
- 【模板】Splay
Splay 均摊复杂度证明见此处 \(\rightarrow\) 链接 代码如下 #include <bits/stdc++.h> using namespace std; const i ...
- 文件拷贝, 使用 BIO,NIO的对比,四种写法性能分析。
测试环境: jdk 1.7 + 2G内存 测试代码基本上复制了: http://blog.csdn.net/tabactivity/article/details/9317143 1 2 3 4 5 ...