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();
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 lines select create cutom type and then afterward you kan group or so anything else { from a in query ..... }
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.
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)
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/