Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter the Application.OpenForms collection with Linq?

Tags:

c#

linq

I would like to use Where to filter the Application.OpenForms collection with Linq, and several questions on this site [1] [2] [3] suggest that all I that need in order to do that is access its AllKeys property. However, even when I create a new Windows Forms application, the collection doesn't seem to have this property at all and doesn't compile when I try to use it. What could be the reason?

like image 887
mzi Avatar asked Sep 10 '14 12:09

mzi


1 Answers

You have to cast it since FormCollection doesn't implement IEnumerable<T> but only IEnumerable:

var query = Application.OpenForms.Cast<Form>()
    .Where(form => ...);

However, the Form has no AllKeys property. Are you confusing webforms and winforms? In the former there is no Application.OpenForms.

like image 169
Tim Schmelter Avatar answered Sep 19 '22 14:09

Tim Schmelter