如何从谓词对象中删除重复项?
让我清楚地解释一下,
我没有搜索字符串,并且我的列表包含不同的字段。
现在我一次不会给出任何搜索字符串,然后我的谓词将使用搜索字符串搜索列表中的每一项。
比赛结束后我将得到一个谓词对象。
对于搜索的下一次迭代,我将得到另一个谓词,它可能是列表中的相同项目,因为我没有在列表的同一字段上搜索。
因此,在收到所有谓词对象后,我将它们组合起来并将其分配给单个谓词对象。
但我遇到了一个例外。
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".
}
该怎么办?如何删除重复值?
Let me explain clearly,
I have no of search strings and my list contains different fields.
Now i will give no of search strings at a time, then my predicate will search each and every item of the list with the search sting.
After the match i will get one predicate object.
For the next iteration of the search i will get another predicate, it may be the same item from list because I'm not searching on the same field of the list.
So after receiving the all predicate objects I'm combining them and assigning it to a single.
But I'm getting an 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".
}
What to do? How can i remove the duplicate values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将强制您的谓词列表是唯一的。
Will force your list of Predicates to be unique.