我可以使用 LINQ 执行以下任一操作吗:
1. 检查 IEnumerable<string>
中的每个元素具有正确的扩展名。如果不是,抛出异常。
foreach(var filepath in filepaths)
if(Path.GetExtension(filepath) != @".xml")
throw new ArgumentException(...);
2.拿一个
IEnumerable<string>
并将其所有元素序列化为一个 string
, 中间有空格。
string args = "";
foreach (var filepath in filepaths)
args += filepath + " ";
谢谢
最佳答案
if (!filepaths.All(x => Path.GetExtension(x) == @".xml"))
{
throw error;
}
string.Join("", filepaths.ToArray())
第二个问题。
关于c# - 我可以用 LINQ 做到这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3922824/