我编写了以下分析csv文件的代码:
var result = FullFile.Split('\n')
.Select(s => new
{ FirstName = s.Split(',')[(int)FirstName.Value],
SirName = s.Split(',')[(int)sirName.Value],
garde = s.Split(',')[(int)Grade.Value] });现在,我用相同的参数和相同的对象多次使用函数Split。
有没有办法继续使用lambada表达式,并减少这个函数调用?
欢迎对我的编码提出任何其他意见。
发布于 2012-12-26 20:15:26
是的,您可以在第一个Select中拆分一次,并将结果向下传递到第二个Select,如下所示:
var result = FullFile
.Split('\n')
.Select(line => line.Split(','))
.Select(tt => new
{ FirstName = tt[(int)FirstName.Value],
SirName = tt[(int)sirName.Value],
garde = tt[(int)Grade.Value] });发布于 2012-12-26 20:19:04
试试这个:
var result = from var s in FullFile.Split('\n')
let x = s.Split(',')
select new {
FirstName = x[(int)FirstName.Value],
SirName = x[(int) SirName.Value],
Grade = x[(int) Grade.Value]
};https://stackoverflow.com/questions/14045714
复制相似问题