[英]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:实现此目的的一种方法是将
Where
与Contains
结合使用,例如:
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.