使用 ! C# 的 Linq Where() 方法中的 (NOT) 操作数

using the ! (NOT) operand in Linq's Where() method for c#

我在数据库中收集了大量水果。

其中一些水果尚未完成,即它们缺少一些关于它们的细节,例如颜色、质地、大小等。在这种情况下,缺少的属性是颜色。

使用 linq 的 Where() 方法我很确定我可以过滤掉所有这些 没有颜色的水果和 return 它们作为集合 水果.

问题是我不确定语法是如何工作的,出于某种原因我 得到尽管 fruitsfruitfinder.fruits 都是的问题 Fruit 集合存在隐式转换问题。

FruitCollection fruits = null;

//Initializing the fruits collection here
//What I want is to only retrive the fruits which have a color
fruits = fruitFinder.fruits.Where( f => f.color != null );

这只是一个示例,但此处提供的答案使问题具有通用性,可以回答我的具体问题。

fruits 将是 IEnumerable<Fruit> 而不是像这样的 FruitCollection

IEnumerable<Fruit> fruits = fruitFinder.fruits.Where( f => f.color != null );

我假设水果类型是 Fruit 但你明白了。