tags:

views:

56

answers:

2

I'm trying to figure out why it does not liking my checks in the All():

itemList.Where(x => itemList.All(x.ItemType != ItemType.Test1 && x.ItemType != ItemType.Test2)).ToList();

The type arguments for method 'System.Linq.Enumerable.All<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' cannot be inferred from the usage.

UPDATED: The original intent here is to give me my list back, filtering out those items where the criterial in the All() match.

So filter out those items in the list where their Item.Type is not this or that.

+1  A: 

The All method needs a Func<T, bool> predicate as an argument. Your code doesn't have one.

It's not clear exactly what your code is meant to do, so it's difficult to suggest any more specific fixes.

LukeH
+3  A: 

You haven't provided a lambda expression - what are you wanting to do with each item in itemList in the inner part? It should be something like: itemList.Where(x => itemList.All(item => x.ItemType != item.Test1 && x.ItemType != item.Test2)).ToList();

Note the "item => " part.

It's unusual to use the same collection again within an All method though... could you give us an idea of the bigger picture?

EDIT: It's not at all clear that you need to call All at all. What's wrong with:

itemList.Where(x => x.ItemType != ItemType.Test1 && x.ItemType != ItemType.Test2)
        .ToList();

? I think you may be a little bit confused about what All is for - it tests whether all the items in the given collection match the given condition.

Jon Skeet
see updated post
CoffeeAddict
Thanks, I assumed I could use All like this...as I have used it before for filtering out a list based on some conditions in the past for another scenario.
CoffeeAddict
hmm, I'm getting nothing back in the list: MyList items = items.Where(x => x.ItemType != ItemType.Item || x.ItemType != DispType.Comment).ToList() as MyList; MyList is just a custom object that inherits from List<MyObject>
CoffeeAddict
May, I'm getting a headache.
CoffeeAddict
Well the list that's being returned isn't going to be an instance of MyList, is it? It's just going to be an instance of `List<MyObject>`. If you want an instance of `MyList`, you'll have to build one.
Jon Skeet