如何使用LINQ返回FileInfo.Name的子如何使用、LINQ、Name、FileInfo

由网友(泪不肯走)分享简介:我想在下面的foreach语句转换为LINQ查询返回的文件名的子字符串成一个列表:的IList<字符串> fileNameSubstringValues​​ =新的名单,其中,串>();//查找与映射文件中的所有组件。ICollection的< FileInfo的>文件= codeToG...

我想在下面的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();
 
C 我用linq联合查询返回一个实体对象,怎么才能返回实体对象

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();

阅读全文

相关推荐

最新文章