让我解释清楚,
我没有搜索字符串,我的列表包含不同的字段。
现在我将一次不给出任何搜索字符串,然后我的谓词将使用搜索字符串搜索列表中的每一项。
比赛结束后,我将获得一个谓词对象。
对于搜索的下一次迭代,我将得到另一个谓词,它可能是列表中的相同项目,因为我不是在列表的同一字段上搜索。
所以在收到所有谓词对象后,我将它们组合起来并将其分配给一个对象。
但是我得到了一个异常(exception)。
string[] separator = new string[] { "+" };
string[] searchItems = s.Split(separator, StringSplitOptions.None);
foreach (string str in searchItems)
{
_defaultPredicate = _defaultPredicate.And(e =>
e.Name.ToLower().Contains(str) ||
e.Lname.ToLower().Contains(str) ||
e.Job.ToLower().Contains(str) );
Predicates.Add(_defaultPredicate);//predicates is a list
}
foreach (Expression<Func<Alert, bool>> Predicate in Predicates)
{
_currentPredicate = _currentPredicate.Or(Predicate);
_currentPredicate.Compile();//Here its giving an exception
// "an item with the same key has already been added".
}
要做什么?如何删除重复值?
最佳答案
foreach (Expression<Func<Alert, bool>> Predicate in Predicates.Distinct())
{
_currentPredicate = _currentPredicate.Or(Predicate);
_currentPredicate.Compile();
}
将强制您的谓词列表是唯一的。
关于c# - 如何从谓词对象中删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6341330/