简体   繁体   English

在文件信息列表中获取文件名列表

[英]Get file name list in fileinfo list

How can i get file name list from 我如何从中获取文件名列表

List<FileInfo> FileInfoList;

by using LINQ? 通过使用LINQ? I want all file names in 我想要所有文件名

List<string> FileNames;

list. 清单。 I don't want to construct a foreach loop and don't want to add file names iteratively. 我不想构造一个foreach循环,也不想迭代地添加文件名。

You can use the Select extension method: 您可以使用Select扩展方法:

List<string> FileNames = FileInfoList.Select(x => x.Name).ToList();

However, if you don't really need it to be a List<T> , you can probably take advantage of Linq's lazy-execution ability by simply omitting the ToList method: 但是,如果您真的不需要将其作为List<T> ,则可以通过简单地省略ToList方法来利用Linq的延迟执行功能:

IEnumerable<string> FileNames = FileInfoList.Select(x => x.Name);

Or if you prefer query syntax: 或者,如果您更喜欢查询语法:

IEnumerable<string> FileNames = from x in FileInfoList select x.Name;

If you only want the FileNames in FileNames list, try: 如果只需要“文件名”列表中的“文件名”,请尝试:

FileInfoList.ForEach(x => 
            FileNames.Add(Path.GetFileName(x.FullName)));

If you want complete file paths in FileNames try 如果要在FileNames中使用完整的文件路径,请尝试

FileInfoList.ForEach(x => 
            FileNames.Add(x.FullName));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM