简体   繁体   English

如何使用 Linq 检查对象列表属性是否允许来自另一个列表的值

[英]How to use Linq to check a list of objects property have allowed values from another list

I have a class Person, in this class i have some properties.我有一个 class 人,在这个 class 中我有一些属性。 One of these properties is string Role.这些属性之一是字符串角色。 the user can input the role in an xml file that i should parse.用户可以在我应该解析的 xml 文件中输入角色。 I have a List of allowedRoles where the allowed roles are hardcoded.我有一个 allowedRoles 列表,其中允许的角色是硬编码的。 I want to check if the Role property and compare it to the allowedRoles list.我想检查 Role 属性并将其与 allowedRoles 列表进行比较。 I want to use linq to filter out the unallowed Roles.我想使用 linq 过滤掉不允许的角色。

public class Person
{
   Public string Name {get; set;}

   Public string Role {get; set;}
}

I have a list with allowed roles我有一个包含允许角色的列表

var allowedRoles = new List<string>{"Developer","Admin","Recruiter"};

At a later stage i when i parsed the xml.在稍后阶段,当我解析 xml 时。 I get a list of Person where i need to validate the role property against my AllowedRoles list.我得到了一个 Person 列表,我需要根据我的 AllowedRoles 列表验证角色属性。 I dont want to use a loop.我不想使用循环。 I would prefer using a linq expression to filter out the unvalid roles where Im left with a list of Persons that have the allowed roles.我更喜欢使用 linq 表达式来过滤掉无效角色,其中我留下了具有允许角色的人员列表。 so I have a list of Person:所以我有一个人员列表:

var persons = new List<Person>();

How do i use my AllowedRoles list to filter out the unallowed Roles from Persons?如何使用我的 AllowedRoles 列表从 Persons 中过滤掉不允许的角色?

One way of achieving this would be to use Where combined with Contains , for example:实现此目的的一种方法是将WhereContains结合使用,例如:

var filteredPeople = persons.Where(p => allowedRoles.Contains(p.Role));

You can simply do:你可以简单地做:

var allowedPersons = persons.Where(p => allowedRoles.Any(r => r == p.Role));

You can use Linq Where extension method with the Contains method of List<string> like that:您可以使用Where扩展方法与List<string>Contains方法,如下所示:

static void Test()
{
  var persons = new List<Person>
  {
    new Person { Name = "1", Role = "Admin" },
    new Person { Name = "2", Role = "Customer" },
    new Person { Name = "3", Role = "Recruiter" },
    new Person { Name = "4", Role = "User" },
  };

  var allowedRoles = new List<string> { "Developer", "Admin", "Recruiter" };

  var personsAllowed = persons.Where(person => allowedRoles.Contains(person.Role));

  foreach ( var person in personsAllowed )
    Console.WriteLine(person.Name);
}

To get unallowed simply use the not operator:要获得不允许,只需使用 not 运算符:

var personsUnallowed = persons.Where(person => !allowedRoles.Contains(person.Role));

The contains method assumes that strings identically matches and are same case. contains 方法假定字符串完全匹配并且大小写相同。

Output allowed Output 允许

1
3

Output unallowed Output 不允许

2
4

With an extension method, you can write it in a more expressive way:使用扩展方法,您可以以更具表现力的方式编写它:

var filteredPeople = persons.Where(p => p.Role.IsIn(allowedRoles));

And your extension method:还有你的扩展方法:

public static class ExtensionMethods
 {
 public static bool IsIn<T>(this T item, List<T> list)
  {
  return list.Contains(item);
  }
 }

This reads nearly like real english: "where role is in allowed roles".这读起来几乎像真正的英语:“角色在允许的角色中”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq-获取具有列表属性的对象,该属性在另一个列表中不包含值 - Linq - get objects with a list property that does not contain values in another list C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list 我可以使用LINQ检查列表中的对象是否具有唯一ID吗? - Can I use LINQ to check if objects in a list have a unique ID? 如何使用linq从另一个列表中的列表中获取总和作为属性 - How to get Sum from List in another List as a property with linq 检查每个包含另一个对象的对象的集合是否包含List的所有值 <string> 通过LINQ - check if collection of objects where each contain another collection of objects contain all values of List<string> via LINQ LINQ根据另一个对象列表从一个对象列表中删除对象 - LINQ to remove objects from a list of objects based on another list of objects 如何根据另一个列表中一个属性的所有值过滤linq查询 - How to filter linq query based on all of the values of one property from another list 使用另一个列表的属性过滤对象列表。 使用linq - Filter a list of objects using a property that is another list. Using linq 如何在值列表中选择所有具有属性值的对象? - How to select all objects that have a property value in list of values? Linq,从对象列表中,根据另一个属性的值创建属性列表 - Linq, from list of objects, create list of properties based on another property's value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM