根据多个选项过滤列表[英] Filter a list based on a multiple options

本文是小编为大家收集整理的关于根据多个选项过滤列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想根据特定的文件扩展名返回文件夹中的文件列表.

目前,我收到所有文件.如何过滤某些类型?

目前,我的代码看起来像这样:

public static List<FileInfo> BuildListOfFiles()
{
    var d = new DirectoryInfo(Constants.FolderToMonitor);
    if (d.Exists)
    {
        var files = (from file in d.EnumerateFiles() select file).ToList();
        return files;
    }
    return null;

}

,但我只想要

*.mp4, *.mpg, *.avi, *.mpeg

我认为我需要另一个地方.但是不确定如何干净地处理多种类型.

推荐答案

您可以使用Linq进行过滤:

var searchExtensions = new string[] { ".mp4", ".mpg", ".avi", ".mpeg" };
var files = d.EnumerateFiles()
             .Where(f => searchExtensions.Contains(Path.GetExtension(file))
             .ToList();

或在查询语法中:

var searchExtensions = new string[] { ".mp4", ".mpg", ".avi", ".mpeg" };
var files = 
    (from file in d.EnumerateFiles()
     where searchExtensions.Contains(Path.GetExtension(file))
     select file)
    .ToList();

另一种解决方案是建立您的文件列表而不是过滤:

var searchPatterns = new string[] { "*.mp4", "*.mpg", "*.avi", "*.mpeg" };
var files = searchPatterns.SelectMany(p => d.EnumerateFiles(p))
                          .ToList();

或在查询语法中:

var searchPatterns = new string[] { "*.mp4", "*.mpg", "*.avi", "*.mpeg" };
var files = 
    (from pattern in searchPatterns
     from file in d.EnumerateFiles(pattern))
     select file)
    .ToList();

其他推荐答案

尝试过这样的方法:

    List<FileInfo> mediaFiles = new List<FileInfo>();

    foreach (string file in Directory.GetFiles(path))
    {
        FileInfo matchFiles = new FileInfo(file);

        if ((matchFiles.Extension == ".mp4") || (matchFiles.Extension == ".mpg") || (matchFiles.Extension == ".avi") || (matchFiles.Extension == ".mpeg"))
        {
            mediaFiles.Add(matchFiles);
        }
    }

其他推荐答案

您可以使用GetFiles()函数

进行此操作
var files = d.GetFiles("*.mp4|*.mpg|*.avi|*.mpeg", SearchOptions.AllDirectories).ToList();

本文地址:https://www.itbaoku.cn/post/1557101.html

问题描述

I want to return a list of files from a folder, based on a specific file extension.

At the moment, I get all files. How do I filter out certain types?

At the moment, my code looks like this:

public static List<FileInfo> BuildListOfFiles()
{
    var d = new DirectoryInfo(Constants.FolderToMonitor);
    if (d.Exists)
    {
        var files = (from file in d.EnumerateFiles() select file).ToList();
        return files;
    }
    return null;

}

But I only want

*.mp4, *.mpg, *.avi, *.mpeg

I think I need another .Where, but not sure how to handle multiple types cleanly.

推荐答案

You can use Linq to do the filtering:

var searchExtensions = new string[] { ".mp4", ".mpg", ".avi", ".mpeg" };
var files = d.EnumerateFiles()
             .Where(f => searchExtensions.Contains(Path.GetExtension(file))
             .ToList();

Or in query syntax:

var searchExtensions = new string[] { ".mp4", ".mpg", ".avi", ".mpeg" };
var files = 
    (from file in d.EnumerateFiles()
     where searchExtensions.Contains(Path.GetExtension(file))
     select file)
    .ToList();

An alternative solution would be to build up your file list rather than filtering it:

var searchPatterns = new string[] { "*.mp4", "*.mpg", "*.avi", "*.mpeg" };
var files = searchPatterns.SelectMany(p => d.EnumerateFiles(p))
                          .ToList();

Or in query syntax:

var searchPatterns = new string[] { "*.mp4", "*.mpg", "*.avi", "*.mpeg" };
var files = 
    (from pattern in searchPatterns
     from file in d.EnumerateFiles(pattern))
     select file)
    .ToList();

其他推荐答案

Tried this way:

    List<FileInfo> mediaFiles = new List<FileInfo>();

    foreach (string file in Directory.GetFiles(path))
    {
        FileInfo matchFiles = new FileInfo(file);

        if ((matchFiles.Extension == ".mp4") || (matchFiles.Extension == ".mpg") || (matchFiles.Extension == ".avi") || (matchFiles.Extension == ".mpeg"))
        {
            mediaFiles.Add(matchFiles);
        }
    }

其他推荐答案

You can do this by using GetFiles() function too

var files = d.GetFiles("*.mp4|*.mpg|*.avi|*.mpeg", SearchOptions.AllDirectories).ToList();