相关疑难解决方法(0)

带linq的子串?

我收集了一些单词,我想从这个集合中创建限量为5个字符的集合

输入:

Car
Collection
Limited
stackoverflow
Run Code Online (Sandbox Code Playgroud)

输出:

car
colle
limit
stack
Run Code Online (Sandbox Code Playgroud)

word.Substring(0,5)抛出异常(长度)

单词.Take(10)也不是好主意

有什么好主意吗?

c# linq string

12
推荐指数
2
解决办法
4万
查看次数

如何使用LINQ返回FileInfo.Name的子字符串

我想将下面的"foreach"语句转换为LINQ查询,该查询将文件名的子字符串返回到列表中:

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);
}
Run Code Online (Sandbox Code Playgroud)

最终结果将类似于以下内容:

IList<string> fileNameSubstringValues = files.LINQ-QUERY-HERE;
Run Code Online (Sandbox Code Playgroud)

.net c# linq

4
推荐指数
1
解决办法
8032
查看次数

标签 统计

c# ×2

linq ×2

.net ×1

string ×1