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();
IList<string> fileNameSubstringValues =
(
from
file in codeToGetFileListGoesHere
select
file.Name.
Substring(0, file.Name.Length -
(file.Name.Length - file.Name.IndexOf(".config.xml"))).ToList();
Enjoy =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With