سؤال

I'm trying to use the following loop

foreach (Form frm in this.MdiChildren)
{
    frm.Close();
}

and transcribe it into a Linq expression like so:

this.MdiParent.MdiChildren.OfType<Form>().ToList().ForEach(x => x.Close());

but this line shows me a NullReferenceException "Object reference not set to an instance of an object"

What am I doing wrong? I am a newbie in Linq.

هل كانت مفيدة؟

المحلول

Try this:

this.MdiChildren.OfType<Form>().ToList().ForEach(x => x.Close());

Unless you are trying that code from one of the children, in which case you try your code

this.MdiParent.MdiChildren.OfType<Form>().ToList().ForEach(x => x.Close());

and it should also work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top