使用 LINQ 读取分隔文件

发布于 2024-10-21 20:35:28 字数 623 浏览 1 评论 0原文

以下 LINQ 读取分隔文件。目前,它仅输出recordId。我希望它输出文件中的所有字段,以便我可以对数据执行一些额外的 LINQ 操作。例如,我想按 recordId 分组,按日期排序,并获取 (x) 结果。

  1. 我希望返回 csv 中的所有字段。我是否需要像对 FirstName、LastName 和 recordId 所做的那样,对变量进行 decalre 并设置使用索引值?没什么大不了的,但是有更好的方法吗?

  2. 我尝试删除 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.

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沫离伤花 2024-10-28 20:35:28

更改您的 Select() 以投影为包含您想要的所有属性的匿名类型:

.Select (record => 
{
  string[] tokens = record.Split('|');

  string FirstName = tokens[2];
  string LastName = tokens[4];
  string recordId = tokens[13];

  return  new { RecordId = recordId, FirstName, LastName };
}

您还可以重写此更简洁的代码:

File.ReadAllLines(path)
    .Select(record  => record.Split('|'))
    .Select(tokens => new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] })
    .GroupBy(x => x.RecordId)
    .Dump();

Change your Select() to project to an anonymous type that holds all the properties you want:

.Select (record => 
{
  string[] tokens = record.Split('|');

  string FirstName = tokens[2];
  string LastName = tokens[4];
  string recordId = tokens[13];

  return  new { RecordId = recordId, FirstName, LastName };
}

you could also rewrite this more succint:

File.ReadAllLines(path)
    .Select(record  => record.Split('|'))
    .Select(tokens => new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] })
    .GroupBy(x => x.RecordId)
    .Dump();
怂人 2024-10-28 20:35:28

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 ..... }

美人如玉 2024-10-28 20:35:28

注意 - 我实际上不确定你想在“分组”中实现什么 - 你真的希望按 ID 分组吗?或者这也是一个 OrderBy 吗?

我认为您可以使用匿名类型实现您正在寻找的目标,例如:

var recipients = File.ReadAllLines(path)
    .Select(record => record.Split('|'))
    .Select(split => new { 
         TheDate = Date.Parse(tokens[0]), // this might be the wrong index?
         FirstName = tokens[2], 
         LastName = tokens[4],
         RecordId = tokens[13],
    }) 
    .OrderBy(anon => anon.TheDate)
    .Dump();

如果您想使用实际类型,那么 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.:

var recipients = File.ReadAllLines(path)
    .Select(record => record.Split('|'))
    .Select(split => new { 
         TheDate = Date.Parse(tokens[0]), // this might be the wrong index?
         FirstName = tokens[2], 
         LastName = tokens[4],
         RecordId = tokens[13],
    }) 
    .OrderBy(anon => anon.TheDate)
    .Dump();

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/

唱一曲作罢 2024-10-28 20:35:28

BrokenGlass 已经回答了这个问题,但我只是想指出这种语法看起来更干净(至少对我来说):

var recipients = File.ReadAllLines(path)
.Select(record => record.Split('|'))
.Select (tokens => 
    new {
        FirstName = tokens[2],
        LastName = tokens[4],
        recordId = tokens[5]
    }
)
.GroupBy(person => person.recordId )
.Dump();

这里实际上并不需要带有语句块的 lambda。

BrokenGlass already answered the question, but I just wanted to point out that this syntax looks cleaner (at least to me):

var recipients = File.ReadAllLines(path)
.Select(record => record.Split('|'))
.Select (tokens => 
    new {
        FirstName = tokens[2],
        LastName = tokens[4],
        recordId = tokens[5]
    }
)
.GroupBy(person => person.recordId )
.Dump();

Theres not really a need for lambdas with statement blocks here.

云醉月微眠 2024-10-28 20:35:28

我将添加:

var recipients = (from record in File.ReadAllLines(path)
    let tokens = record.Split('|')
    let record2 = new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] }
    group record2 by record2.RecordId
).Dump();

在 LINQ 非功能语法中。

我将添加另一个变体,它使用 select ... into 而不是 let

var recipients = (from record in File.ReadAllLines(path)
    select record.Split('|') into tokens
    select new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] } into record2
    group record2 by record2.RecordId
).Dump();

您需要对它们进行基准测试以找到最快的(这肯定会很有趣......明天早上我会尝试)

I'll add:

var recipients = (from record in File.ReadAllLines(path)
    let tokens = record.Split('|')
    let record2 = new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] }
    group record2 by record2.RecordId
).Dump();

in LINQ non-functional syntax.

I'll add another variant, that uses select ... into instead of let.

var recipients = (from record in File.ReadAllLines(path)
    select record.Split('|') into tokens
    select new { RecordId = tokens[13], FirstName = tokens[2], LastName = tokens[4] } into record2
    group record2 by record2.RecordId
).Dump();

You would need to benchmark them to find the fastest (and it would surely be interesting... Tomorrow morning I'll try)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文