Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

too many function calls

Tags:

c#

lambda

linq

I wrote the following code parsing a csv file:

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] });

Now, I use the function Split too many times with the same arguments, and on the same object.

Is there a way to carry on using the lambada expression, and cut down this function calls?

Any other comments on my coding are welcome

like image 784
elyashiv Avatar asked Aug 31 '26 06:08

elyashiv


1 Answers

Yes, you can split once in the first Select, and pass the result down the chain to the second Select, like this:

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] });
like image 76
Sergey Kalinichenko Avatar answered Sep 02 '26 18:09

Sergey Kalinichenko