How to tell "OfType()" to ignore inherited classes?

In my WPF application, I have the following line:

var windowList = Application.Current.Windows.OfType<Window>().ToList();

This is returning 2 results. These results have the following types:

  1. Microsoft.Crm.UnifiedServiceDesk.Desktop
  2. System.Windows.Window

I only want the 2nd result to be returned. However, since result #1 inherits System.Windows.Window, it is included in the result.

Is there a way to restrict the OfType<T>() function to NOT return inherited classes, or do I have to accomplish this another way?

Jon Skeet
people
quotationmark

You can't use OfType on its own to achieve this, but you can use that with a Where clause afterwards:

var windowList = Application.Current.Windows
    .OfType<Window>()
    .Where(w => w.GetType() == typeof(Window))
    .ToList();

people

See more on this question at Stackoverflow