过滤对象 ObservableCollection;某种类型的

发布于 2024-11-18 23:15:24 字数 885 浏览 2 评论 0原文

我有一个 ObservableCollection,它包含两种不同类型的对象:DirectoryFile。该集合绑定到一个控件,有时我想过滤掉 File

我有以下代码,但不起作用:

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

我收到此错误:

无法转换类型的对象 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]' 输入“CMBraga_FileExplorer.File”。

我怎样才能得到我的价值观?我知道它们是File

// 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}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-11-25 23:15:24

我怀疑您收到异常的原因是因为您声明了查询变量的类型(即 from File f in ...)。通过这样做,您正在尝试将对象转换为指定的类型。看起来 d.Childs 中某些项目的类型是 ObservableCollection。这些显然不是 File 对象,因此会出现强制转换错误。

使用 Enumerable.OfType() 扩展这里的方法进行过滤。这正是它的用途。

var files = selectedDirs
    .Cast<Directory>() // this cast might not even be necessary
    .SelectMany(d => d.Childs)
    .OfType<File>(); // filter selecting only File objects

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
简单爱 2024-11-25 23:15:24

您可以使用 Linq 表达式:

using System.Linq;
// ...
ObservableCollection<object> list;
// ...
IEnumerable<CMBraga_FileExplorer.File> castedList = list.Cast<CMBraga_FileExplorer.File>();

You can use Linq expression for that:

using System.Linq;
// ...
ObservableCollection<object> list;
// ...
IEnumerable<CMBraga_FileExplorer.File> castedList = list.Cast<CMBraga_FileExplorer.File>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文