How to remove duplicates from a predicate object?
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开发者_JAVA百科 duplicate values?
foreach (Expression<Func<Alert, bool>> Predicate in Predicates.Distinct())
{
_currentPredicate = _currentPredicate.Or(Predicate);
_currentPredicate.Compile();
}
Will force your list of Predicates to be unique.
精彩评论