如何使用LINQ返回FileInfo.Name的子 [英] How to use LINQ to return substring of FileInfo.Name
本文介绍了如何使用LINQ返回FileInfo.Name的子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在下面的foreach语句转换为LINQ查询返回的文件名的子字符串成一个列表:
的IList<字符串> fileNameSubstringValues =新的名单,其中,串>();
//查找与映射文件中的所有组件。
ICollection的< FileInfo的>文件= codeToGetFileListGoesHere;
//解析的文件名来获取程序集名称。
的foreach(在文件的FileInfo文件)
{
字符串文件名= file.Name.Substring(0,file.Name.Length - (file.Name.Length - file.Name.IndexOf(config.xml中。)));
fileNameSubstringValues.Add(文件名);
}
最终的结果将是类似于以下内容:
的IList<字符串> fileNameSubstringValues = files.LINQ查询-HERE;
解决方案
尝试是这样的:
VAR的fileList = files.Select(文件=>
file.Name.Substring(0,file.Name.Length -
(file.Name.Length - file.Name.IndexOf(。config.xml的))))
.ToList();
I would like to convert the below "foreach" statement to a LINQ query that returns a substring of the file name into a list:
IList<string> fileNameSubstringValues = new List<string>();
//Find all assemblies with mapping files.
ICollection<FileInfo> files = codeToGetFileListGoesHere;
//Parse the file name to get the assembly name.
foreach (FileInfo file in files)
{
string fileName = file.Name.Substring(0, file.Name.Length - (file.Name.Length - file.Name.IndexOf(".config.xml")));
fileNameSubstringValues.Add(fileName);
}
The end result would be something similar to the following:
IList<string> fileNameSubstringValues = files.LINQ-QUERY-HERE;
解决方案
Try something like this:
var fileList = files.Select(file =>
file.Name.Substring(0, file.Name.Length -
(file.Name.Length - file.Name.IndexOf(".config.xml"))))
.ToList();
这篇关于如何使用LINQ返回FileInfo.Name的子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文