How to remove duplicates from a predicate object?

Let me explain

I do not have search strings and my list contains different fields.

Now I will not specify the search lines at a time, then my predicate will search for each element of the list using a search.

After the match, I get one predicate.

For the next iteration of the search, I will get another predicate, it may be the same element from the list, because I'm not looking in the same list box.

So, after receiving all the predicate objects, I combine them and assign them to one.

But I get 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 to remove duplicate values?

+3
source share
1 answer
foreach (Expression<Func<Alert, bool>> Predicate in Predicates.Distinct())    
    {
        _currentPredicate = _currentPredicate.Or(Predicate);
        _currentPredicate.Compile();    
    }

Makes your predicate list unique.

0
source

All Articles