开发者

LINQ statement All checks syntax problem

开发者 https://www.devze.com 2022-12-13 14:22 出处:网络
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();

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 i开发者_如何学运维ntent 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号