使用 LINQ 读取分隔文件
以下 LINQ 读取分隔文件。目前,它仅输出recordId。我希望它输出文件中的所有字段,以便我可以对数据执行一些额外的 LINQ 操作。例如,我想按 recordId 分组,按日期排序,并获取 (x) 结果。
我希望返回 csv 中的所有字段。我是否需要像对 FirstName、LastName 和 recordId 所做的那样,对变量进行 decalre 并设置使用索引值?没什么大不了的,但是有更好的方法吗?
我尝试删除 return 语句并使用 new 进行投影,但这不起作用。
有什么建议吗?
谢谢!
var recipients = File.ReadAllLines(path)
.Select (record =>
{
string[] tokens = record.Split('|');
string FirstName = tokens[2];
string LastName = tokens[4];
string recordId = tokens[13];
return recordId;
}
)
.GroupBy (recordId => {return recordId; } )
.Dump();
The following LINQ reads a delimited file. Currently, it outputs only the recordId. I want it to output all the fields in file so I can perform some additional LINQ operations on the data. For example, I want to group by recordId, sort by a date, and take(x) results.
I want all the fields in the csv to be returned. Do I need to decalre a variable and set use the index value, like I did for FirstName, LastName and recordId? Not a big deal but is there a better way?
I tried removing the return statement and projecting with new but that didn't work.
Any suggestions?
Thanks!
var recipients = File.ReadAllLines(path)
.Select (record =>
{
string[] tokens = record.Split('|');
string FirstName = tokens[2];
string LastName = tokens[4];
string recordId = tokens[13];
return recordId;
}
)
.GroupBy (recordId => {return recordId; } )
.Dump();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更改您的
Select()
以投影为包含您想要的所有属性的匿名类型:您还可以重写此更简洁的代码:
Change your
Select()
to project to an anonymous type that holds all the properties you want:you could also rewrite this more succint:
var query = from l in rows select 创建自定义类型,然后您可以分组或其他任何内容 { from a in query ..... }
var query = from l in lines select create cutom type and then afterward you kan group or so anything else { from a in query ..... }
注意 - 我实际上不确定你想在“分组”中实现什么 - 你真的希望按 ID 分组吗?或者这也是一个 OrderBy 吗?
我认为您可以使用匿名类型实现您正在寻找的目标,例如:
如果您想使用实际类型,那么 Jon Skeet 的这篇文章可能会有所帮助作为背景 - http://www.developerfusion.com/article/84468/linq-to-log-files/
Note - I'm not actually sure what you are trying to achieve in your "Grouping" - are you really looking to Group by the Id? Or is this an OrderBy too?
I think you can achieve what you are looking for using anonymous types, e.g.:
If you want to use an actual type, then this article from Jon Skeet might help as a background - http://www.developerfusion.com/article/84468/linq-to-log-files/
BrokenGlass 已经回答了这个问题,但我只是想指出这种语法看起来更干净(至少对我来说):
这里实际上并不需要带有语句块的 lambda。
BrokenGlass already answered the question, but I just wanted to point out that this syntax looks cleaner (at least to me):
Theres not really a need for lambdas with statement blocks here.
我将添加:
在 LINQ 非功能语法中。
我将添加另一个变体,它使用
select ... into
而不是let
。您需要对它们进行基准测试以找到最快的(这肯定会很有趣......明天早上我会尝试)
I'll add:
in LINQ non-functional syntax.
I'll add another variant, that uses
select ... into
instead oflet
.You would need to benchmark them to find the fastest (and it would surely be interesting... Tomorrow morning I'll try)