3

I'm trying do the next instruction in c#:

Form form = this.MdiChildren.FirstOrDefault(x => x is Type.GetType("MyFormName"));

But i have a error: Method name expected.

What would be the correct usage of the instruction.

1
  • 1
    If this question is about the particular error message, it is not related to lambda expressions - in that case, you should reduce the problem to something like bool a = x is Type.GetType("MyFormName");. Feb 19, 2014 at 19:07

1 Answer 1

7

Since you have a Type instance you need to use IsAssignableFrom instead of is:

x => Type.GetType("MyFormName").IsAssignableFrom(x.GetType())

This of course assumes that you really can't reference the actual type at compile time. If you can, then you could instead simplify this code to:

.OfType<MyFormName>().FirstOrDefault();

Which is going to have something internally that resembles:

x is MyFormName

Which is how the is operator is designed to be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.