如何创建一个获取除特定值之外的所有内容的linq查询

时间:2021-03-17 14:54:48

Say I have a linq query select r in db.Roles where r.RoleName DOES NOT contain ("Administrator") select r;

假设我在db.Roles中有一个linq查询选择r,其中r.RoleName不包含(“Administrator”)select r;

It is the does not contain part that has me confused. I realize I can do that .Contains call but how do you do the opposite?

这是不包含让我困惑的部分。我意识到我能做到这一点。包含电话,但你怎么做相反的事情呢?

Thanks!

Update: I found the Exclude method and here is how I used it:

更新:我找到了Exclude方法,以下是我如何使用它:

var role = (from r in db.Roles
            orderby r.RoleName
            select r)
           .Except(from r in db.Roles 
                   where r.RoleName == "Administrator" & r.RoleName == "DataEntry" 
                   select r
            );

2 个解决方案

#1


Try the following

请尝试以下方法

var query = db.Roles.Where(x => !x.RoleName.Contains("Administrator"));

You can just use the C# not operator ! (exclamation point). Expanded LINQ syntax version

你可以使用C#not操作符! (感叹号)。扩展LINQ语法版本

var query = 
  from it in db.Roles
  where !it.RoleName.Contains("Administrator")
  select it;

#2


If you had multiple roles to exclude, you might take a look at the Except function.

如果要排除多个角色,可以查看“除外”功能。

#1


Try the following

请尝试以下方法

var query = db.Roles.Where(x => !x.RoleName.Contains("Administrator"));

You can just use the C# not operator ! (exclamation point). Expanded LINQ syntax version

你可以使用C#not操作符! (感叹号)。扩展LINQ语法版本

var query = 
  from it in db.Roles
  where !it.RoleName.Contains("Administrator")
  select it;

#2


If you had multiple roles to exclude, you might take a look at the Except function.

如果要排除多个角色,可以查看“除外”功能。