我使用MVVM用DirectoryInfo及其所有子目录和文件填充TreeView.

然后我想和LINQ一起在树视图中搜索.where(),但我只搜索顶级目录,而不是文件.

I'm creating the treeview/DirectoryInfo with a recursive method:

  public class ItemProvider
    {
        public ObservableCollection<Item> GetItems(string path)
        {
            var items = new ObservableCollection<Item>();

            if (System.IO.Directory.Exists(path))
            {
                

                var dirInfo = new DirectoryInfo(path);

                foreach (var directory in dirInfo.EnumerateDirectories())
                {
                    var item = new DirectoryItem
                    {
                        Name = directory.Name,
                        Path = directory.FullName,
                        Items = GetItems(directory.FullName)
                    };

                    items.Add(item);
                }

                foreach (var file in dirInfo.EnumerateFiles())
                {
                    InteropBitmap thumbImg = ShellFile.FromFilePath(file.FullName).Thumbnail.BitmapSource as InteropBitmap;
                    thumbImg.Freeze();

                    var item = new FamilyItem
                    {
                        Name = file.Name,
                        Path = file.FullName,
                        FamImage = thumbImg
                    };

                    if (item.Name.Contains(".rfa"))
                    {

                    items.Add(item);

                    }

                }

                return items;
            }
            else
            {

                return items;
            }
        }
            
       

DirectoryItem和FamilyItem都继承了一个名为Item的模型:

Item model:

public class Item
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public InteropBitmap FamImage { get; set; }
    }

DirectoryItem model:

public class DirectoryItem : Item

    {
        public ObservableCollection<Item> Items { get; set; }
    
        public DirectoryItem()
        {
        Items = new ObservableCollection<Item>();
        }
    }

FamilyItem:

 public class FamilyItem : Item
    {

    }

I'm filtering the collection with:

Items = new ObservableCollection<Item>(ItemsCache.Where(x => x.Name.ToLower().Contains(SearchText.ToLower())));

我只得到匹配的顶级目录-我想得到所有匹配的"end"文件-我该怎么做呢?

推荐答案

您可以通过递归版本重写.Where(..):

   public static class ExtenstioMethods
   {
      public static IEnumerable<Item> Where<Item>(this IEnumerable<Item> source, Func<Item, bool> predicate)
      {
         var result = new List<Item>();
         foreach (var item in source)
         {
            if ((item is FamilyItem) && predicate(item))
            {
               result.Add(item);
            }
            else if (item is DirectoryItem)
            {
               var newSourc = ((item as DirectoryItem).Items) as IEnumerable<Item>;
               result = result.Union(newSourc.Where(predicate)).ToList();
            }
         }
         return result;
      }
   }

Csharp相关问答推荐

为什么我的ASP.NET核心MVC应用程序要为HTML元素添加一些标识符?

Blazor EventCallback<;MyType<;T>;>;

将现有字段映射到EFCore中的复杂类型

在命名管道上使用GRPC ASP.NET核心时如何配置命名管道权限

使用两个不同的枚举作为Switch语句中的CASE生成唯一值

BlockingCollection T引发意外InvalidOperationException

正确处理嵌套的本机集合

使用ExtractIconEx(或其他方式)提取最大的可用图标

关于扩展文件类C#的矛盾

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

正在从最小API-InvocationConext.Arguments中检索参数的FromBodyAttribute

是否可以从IQueryable T中获取一个IdentyEntry T>

使用可空引用类型时C#接口实现错误

Foreach非常慢的C#

Xamarin.Forms-如何创建可 Select 的显示alert 或弹出窗口?

并发表更新.EF核心交易

NETSDK1201:对于面向.NET 8.0和更高版本的项目,默认情况下,指定RUNTIME标识符将不再生成自包含的应用程序

如何对列表<;列表>;使用集合表达式?

从API调用调用Worker服务方法

如何在C#中加载证书包?