C # - Error CS1928: checking a list item with a derived class

I have a custom class derived from a library (Satsuma) as follows:

public class DCBaseNode : Node { public bool selected = false; } 

and Neighbors in the library, which returns a List<Node> . I want to be able to do this:

 graph.Neighbors(theNode).Any(n => n.selected == true); 

But Any sees n as Node , not DCBaseNode , so it doesn't understand .selected .

So I tried:

 graph.Neighbors(theNode).Any<DCBaseNode>(n => n.selected == true); 

... which gives me this error:

Error CS1928: type System.Collections.Generic.List<Satsuma.Node>' does not contain a member Any' and is the best extension overload method. System.Linq.Enumerable.Any (this System.Collections.Generic.IEnumerable, System.Func) 'has some invalid arguments

... but I do not understand how the arguments are invalid.

+6
source share
1 answer

Looks like you need to knock down ...

 graph.Neighbors(theNode) .OfType<DCBaseNode>() .Any(n => n.selected); 
+6
source

Source: https://habr.com/ru/post/973784/


All Articles