0

What is the problem with the code below?

var newContextElementCollection = new List<NewContextElements>();  
var res = from eleCollection in newContextElementCollection                     
          where eleCollection.Property.Except(new List<string> { "Password", "Some Other Value" })
          select eleCollection;

The NewContextElementCollection class:

public class NewContextElements
{
    public string Property { get; set; }    
    public string Value { get; set; }
}

I am getting this error:

Instance argument: cannot convert from 'string' to 'System.Linq.IQueryable' Error 2 'string' does not contain a definition for 'Except' and the best extension method overload 'System.Linq.Queryable.Except(System.Linq.IQueryable,

System.Collections.Generic.IEnumerable)' has some invalid arguments

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27

2 Answers2

0

Property is a string, not an IEnumerable<string>, which is what your Except clause can work on.

var res= from eleCollection in newContextElementCollection
     where eleCollection.Property !=  "Password"
     select eleCollection;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • That is correct. Actually the exclude list will be a collection and not a scalar item. Like new List { "Password","Some other value" }. How to handle this? –  May 10 '12 at 09:18
  • The example you shown has similar Entity type. In my case, it is however different. In such a case it won't fit (i think so) –  May 10 '12 at 09:24
  • 1
    @user1323981: then the problem is that `Except` can not serve as a predicate, because is does not return a boolean. You can use it in a select clause, `select eleCollection.Property.Except...` – Gert Arnold May 10 '12 at 10:03
0
var excluded = new List<string> { "Password","Some Other Value" };
var res = newContextElementCollection.Where(m => !excluded.Contains(m.Property));
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122