Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:01:19+00:00 2026-05-23T15:01:19+00:00

I have a ObservableCollection<Object> that holds two different types of objects: Directory and File

  • 0

I have a ObservableCollection<Object> that holds two different types of objects: Directory and File. This collection is bound to a control and at some time, I want to filter out the Files.

I have the following code, which is not working:

var files = (from File f in (from Directory d in selectedDirs
                             select d.Childs)
             where f is File
             select f);

I’m getting this error:

Unable to cast object of type
‘System.Collections.ObjectModel.ObservableCollection`1[System.Object]’
to type ‘CMBraga_FileExplorer.File’.

How can I get my values? I know they are Files.

// this was ran without explicit conversion ( just as an example )

? myCollection
Count = 5
    [0]: {CMBraga_FileExplorer.File}
    [1]: {CMBraga_FileExplorer.File}
    [2]: {CMBraga_FileExplorer.File}
    [3]: {CMBraga_FileExplorer.File}
    [4]: {CMBraga_FileExplorer.File}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:01:20+00:00Added an answer on May 23, 2026 at 3:01 pm

    I suspect the reason you’re getting the exception is because you are declaring the types of your query variable (i.e., from File f in ...). By doing that, you are trying to cast the objects to the specified type. It would appear that your types of some of the items in d.Childs are ObservableCollection<Object>. Those are clearly not File objects hence the cast error.

    Use the Enumerable.OfType() extension method here to do the filtering. This is exactly what it was made for.

    var files = selectedDirs
        .Cast<Directory>() // this cast might not even be necessary
        .SelectMany(d => d.Childs)
        .OfType<File>(); // filter selecting only File objects
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid that's bound to an observablecollection object. The collection may be
I have this ListBox which is bound to an ObservableCollection. Each object in the
I'm stumped here. I have an observable collection that holds business objects. I have
Scenario I have a TreeView that is bound to ObservableCollection<T> . The collection gets
I have an ItemsControl bound to an ObservableCollection. When the observable collection changes, I
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a collection ObservableCollection<Foo> , and I want to bind this to a
I have an ObservableCollection, which holds a Person object. I have a search feature
I have created an object called Project that has different properties (strings and some
I have a combobox that is bound to an observablecollection of strings. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.