你可以这样做:
var splits = input.Select(v => v.Split('@'));
var features = splits.Select(s => s[0]).ToList();
var projects = splits.Select(s => s[1]).ToList();
如果您不介意更多代码但更好的性能和更少的垃圾收集器压力,那么:
var features = new List<string>();
var projects = new List<string>();
foreach (var split in input.Select(v => v.Split('@')))
{
features.Add(split[0]);
projects.Add(split[1]);
}
但总的来说,我建议创建类并解析您的输入(更多 C# 风格的方法):
public class ProjectFeature
{
public readonly string Project;
public readonly string Feature;
public ProjectFeature(string project, string feature)
{
this.Project = project;
this.Feature = feature;
}
public static IEnumerable<ProjectFeature> ParseList(IEnumerable<string> input)
{
return input.Select(v =>
{
var split = v.Split('@');
return new ProjectFeature(split[1], split[0]);
}
}
}
并在以后使用它(只是可能使用的一个例子):
var projectFeatures = ProjectFeature.ParseList(File.ReadAllLines(@"c:\features.txt")).ToList();
var features = projectFeatures.Select(f => f.Feature).ToList();
var projects = projectFeatures.Select(f => f.Project).ToList();
// ??? etc.