C# 筛选类字符串属性
我想按任意顺序筛选classC# 筛选类字符串属性,c#,regex,linq,C#,Regex,Linq,我想按任意顺序筛选classUserstring属性 我的User类如下所示: internal class User { public string FirstName { get; } public string LastName { get; } public string Email { get; } public User(string firstName, string lastName, string email) { Fi
User
string属性
我的User
类如下所示:
internal class User
{
public string FirstName { get; }
public string LastName { get; }
public string Email { get; }
public User(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}
public override string ToString()
{
return $"{FirstName} {LastName} - {Email}";
}
}
private readonly List<User> _userList = new List<User>();
public Form1()
{
InitializeComponent();
_userList.Add(new User("Zar", "Nikolaus II", "ruler@stackoverflow.bom"));
_userList.Add(new User("Tommy", "Hilfiger", "polo@stackoverflow.bom"));
_userList.Add(new User("Nina", "Richy", "thin-thing@stackoverflow.bom"));
_userList.Add(new User("Armin", "Van Buren", "singer@stackoverflow.bom"));
_userList.Add(new User("Lauren", "Ralph", "polo@stackoverflow.bom"));
_userList.Add(new User("Van der Vaart", "(Marin-) Rafael", "soccer@stackoverflow.bom"));
_userList.Add(new User("Tommy", "Vercetti", "character@stackoverflow.bom"));
}
private void FilterIt()
{
var filter = textBox1.Text;
var properties = typeof(User).GetProperties();
var filteredUsers = this._userList.Where(user => properties.Any(propInfo => Regex.Split(propInfo.GetValue(user).ToString(), @"\W").Any(w => filter.Contains(w))));
}
我的过滤算法如下所示:
internal class User
{
public string FirstName { get; }
public string LastName { get; }
public string Email { get; }
public User(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}
public override string ToString()
{
return $"{FirstName} {LastName} - {Email}";
}
}
private readonly List<User> _userList = new List<User>();
public Form1()
{
InitializeComponent();
_userList.Add(new User("Zar", "Nikolaus II", "ruler@stackoverflow.bom"));
_userList.Add(new User("Tommy", "Hilfiger", "polo@stackoverflow.bom"));
_userList.Add(new User("Nina", "Richy", "thin-thing@stackoverflow.bom"));
_userList.Add(new User("Armin", "Van Buren", "singer@stackoverflow.bom"));
_userList.Add(new User("Lauren", "Ralph", "polo@stackoverflow.bom"));
_userList.Add(new User("Van der Vaart", "(Marin-) Rafael", "soccer@stackoverflow.bom"));
_userList.Add(new User("Tommy", "Vercetti", "character@stackoverflow.bom"));
}
private void FilterIt()
{
var filter = textBox1.Text;
var properties = typeof(User).GetProperties();
var filteredUsers = this._userList.Where(user => properties.Any(propInfo => Regex.Split(propInfo.GetValue(user).ToString(), @"\W").Any(w => filter.Contains(w))));
}
private readonly List_userList=new List();
公共表格1()
{
初始化组件();
_添加(新用户(“Zar”、“Nikolaus II”、”ruler@stackoverflow.bom"));
_添加(新用户(“Tommy”、“Hilfiger”、”polo@stackoverflow.bom"));
_添加(新用户(“Nina”、“Richy”、“thin-thing@stackoverflow.bom"));
_添加(新用户(“Armin”、“Van Buren”、”singer@stackoverflow.bom"));
_添加(新用户(“Lauren”、“Ralph”、”polo@stackoverflow.bom"));
_添加(新用户(“Van der Vaart”,“Marin-”Rafael“,”soccer@stackoverflow.bom"));
_添加(新用户(“Tommy”、“Vercetti”、”character@stackoverflow.bom"));
}
私有void FilterIt()
{
var filter=textBox1.Text;
var properties=typeof(User).GetProperties();
var filteredUsers=this.\u userList.Where(user=>properties.Any(propInfo=>Regex.Split(propInfo.GetValue(user.ToString(),@“\W”).Any(W=>filter.Contains(W)));
}
现在,我的目标是使用以下过滤器获得以下结果:
过滤器:Tom
似乎唯一的诀窍是在匹配时将
ToString()
-FirstName
,LastName
,电子邮件
组合:
string filter = "rafa va";
// We assume that
// 1. Patterns are separated by space (or tabulation)
// 2. Patterns should be matched in any order
// 3. Case should be ignored
var patterns = filter
.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
.Select(item => Regex.Escape(item)) // <- comment it out if you want to allow, say, \d+
.ToArray();
var result = _userList
.Where(user => patterns
.All(pattern => Regex.IsMatch(user.ToString(), pattern, RegexOptions.IgnoreCase)));
string filter=“rafa va”;
//我们假设
// 1. 图案以空格(或表格)分隔
// 2. 模式应该以任何顺序匹配
// 3. 这种情况应该被忽略
变量模式=过滤器
.Split(新字符[]{'','\t'},StringSplitOptions.RemoveEmptyEntries)
.Select(item=>Regex.Escape(item))//模式
.All(pattern=>Regex.IsMatch(user.ToString(),pattern,RegexOptions.IgnoreCase));
好的,你哪里有问题?我首先将“使用反射进行操作”与“使用正则表达式匹配字符串”区分开来。它们是正交的,所以你的问题应该是其中一个,而且只有一个。(在这两种情况下,a都很有用-不需要UI。)