" NOT IN"子句中的LINQ到实体 [英] "NOT IN" clause in LINQ to Entities

查看:115
本文介绍了" NOT IN"子句中的LINQ到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正我不能在第一个创建像我将不得不在SQL Server中的 LINQ到实体的?

Is there anyway I can create a not in clause like I would have in SQL Server in Linq to Entities?

推荐答案

如果您使用的是内存中的集合作为过滤器,它可能是最好使用包含的否定()。请注意,如果列表太长,这可能会失败,在这种情况下,你需要选择另一种策略(参见下面的使用策略完全DB-导向查询)。

If you are using an in-memory collection as your filter, it's probably best to use the negation of Contains(). Note that this can fail if the list is too long, in which case you will need to choose another strategy (see below for using a strategy for a fully DB-oriented query).

   var exceptionList = new List<string> { "exception1", "exception2" };

   var query = myEntities.MyEntity
                         .Select(e => e.Name)
                         .Where(e => !exceptionList.Contains(e.Name));

如果您是基于使用另一个数据库查询除排除可能是一个更好的选择。 (这里是一个链接在LINQ到实体支持的扩展集)

If you're excluding based on another database query using Except might be a better choice. (Here is a link to the supported Set extensions in LINQ to Entities)

   var exceptionList = myEntities.MyOtherEntity
                                 .Select(e => e.Name);

   var query = myEntities.MyEntity
                         .Select(e => e.Name)
                         .Except(exceptionList);

这假定您在其中不包括根据其它表的某些属性特定的人,并希望未排除的实体的名称复杂的实体。如果你想整个实体,那么你需要构建例外,这样它们能够满足默认的相等操作符(见的文档)。

This assumes a complex entity in which you are excluding certain ones depending some property of another table and want the names of the entities that are not excluded. If you wanted the entire entity, then you'd need to construct the exceptions as instances of the entity class such that they would satisfy the default equality operator (see docs).

这篇关于&QUOT; NOT IN&QUOT;子句中的LINQ到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆