3

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?

Sowmya
  • 345
  • 1
  • 5
  • 18
  • I think a short but *complete* example would help us here. It's not really clear what it means at the moment. If you just want an "in" check, is there any reason you're not just using `Contains`? – Jon Skeet Jun 14 '11 at 09:07
  • ya because i don't want the exact match. If the list contains the string then it should display. – Sowmya Jun 14 '11 at 09:11
  • But that's what `x => list.Contains(x)` would do... again, a complete example might make this clearer. – Jon Skeet Jun 14 '11 at 09:13
  • what *exactly* is throwing that exception? what line of code? I'm not sure it is anything to do with the expressions... – Marc Gravell Jun 14 '11 at 09:15
  • 1
    Please update the question with this extra information, it is unreadable in its comment form. – Richard Jun 14 '11 at 09:34

1 Answers1

0
foreach (Expression<Func<Alert, bool>> Predicate in Predicates.Distinct())    
    {
        _currentPredicate = _currentPredicate.Or(Predicate);
        _currentPredicate.Compile();    
    }

Will force your list of Predicates to be unique.

The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55