使用 LINQ 和 File.ReadAllLines() 读取下一行
我有一个代表项目的文件,一行中有项目 GUID,后跟 5 行描述该项目。
示例:
Line 1: Guid=8e2803d1-444a-4893-a23d-d3b4ba51baee name= line1
Line 2: Item details = bla bla
.
.
Line 7: Guid=79e5e39d-0c17-42aa-a7c4-c5fa9bfe7309 name= line7
Line 8: Item details = bla bla
.
.
我尝试首先访问此文件,以获得符合使用 LINQ 提供的条件的项目 GUID,例如 where line.Contains("line1").. 这样我将获得整行,我将从中提取 GUID在那里,我想将此 GUID 传递给另一个应该“再次”访问该文件的函数,找到该行(其中 line.Contains("line1") && line.Contains("8e2803d1-444a-4893 -a23d-d3b4ba51baee")
并从该行开始读取接下来的 5 行。
有没有有效的方法可以做到这一点?
I have a file which represents items, in one line there's Item GUID followed by 5 lines describing the item.
Example:
Line 1: Guid=8e2803d1-444a-4893-a23d-d3b4ba51baee name= line1
Line 2: Item details = bla bla
.
.
Line 7: Guid=79e5e39d-0c17-42aa-a7c4-c5fa9bfe7309 name= line7
Line 8: Item details = bla bla
.
.
I am trying to access this file first to get the GUIDs of the items meet the criteria provided using LINQ e.g. where line.Contains("line1").. This way I will get the whole line, I will extract the GUID from there, I want to pass this GUID to another function which should access the file "again", find that line (where line.Contains("line1") && line.Contains("8e2803d1-444a-4893-a23d-d3b4ba51baee")
and reads the next 5 lines starting from that line.
Is there any efficient way to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到您需要执行的操作的要求以及数组中行的索引是完美积分,我认为完全使用 LINQ 确实没有意义。我还建议一次完成所有操作 - 多次打开文件不会像只读取所有内容并立即处理它那样有效。只要文件的结构符合您的描述,这不会非常困难:
I don't think it really makes sense to use LINQ entirely given the requirements of what you need to do and given that the index of the line in the array is fairy integral. I would also recommend doing everything in one pass - opening the file multiple times won't be as efficient as just reading everything once and processing it immediately. As long as the file is structured as well as you describe, this won't be terribly difficult:
我尝试了几种不同的方法来使用 LINQ 执行此操作,但没有任何方法允许我对文件进行单次扫描。对于您正在谈论的这种情况,我将进入可枚举级别并使用 GetEnumerator,如下所示:
I tried a couple different ways to do this with LINQ but nothing allowed me to do a single scan of the file. For this scenario you're talking about I would go down to the Enumerable level and use the GetEnumerator like this: