If you want to use LINQ for this efficiently, you will need to use expression trees in a similar (but much simpler) way than what various LINQ providers for SQL databases do. Although it is possible, I think that for such a simple task there would be a lot of code.
Because of this, I believe that the best solution would be to use a separate method to select the desired rows (and then, possibly, LINQ to work with the result).
In addition, many operations that return collections (including the source code and my modification) can be simplified using iterator methods .
So your code might look something like this:
public static IEnumerable<CSVRow> GetRows( this CSVReader reader, int idGreaterThan, int idLessThan) { for (int i = idGreaterThan + 1; i < idLessThan; i++) { yield return new CSVRow(reader, i); } }
Here it is an extension method for CSVReader , but another solution (e.g. the actual method for this class) may be more suitable for you.
Your example would look something like this:
max = csvReader.GetRows(100, 150).Max(y => y["A"]);
(Also, it seems strange to me that when you have limits of 100 and 150, you really need lines between 101 and 149. But I assume that you have a reason for this, so I did the same.)
source share