I'm having a bit of trouble selecting from a data table using LINQ. The results would map to a class TabResult
. The class looks like this:
public class TabResult
{
public decimal Value;
public string LineNumber;
}
Now I'm trying to add these results to an array of IEnumerable<TabResult>
, one index of the array per sheet.
This was my first attempt in code:
public LCRResultSet(DataTable results)
{
for (int i = 0, sheet = 72; i < 3; i++, sheet++)
{
_results[i] = from tab in results.AsEnumerable()
where tab.Field<string>("LCR_SHEET_NUMBER") == sheet.ToString()
select new TabResult { LineNumber = tab.Field<string>("LCR_LINE_NUMBER"), Value = tab.Field<decimal>("VALUE") };
}
}
It works okay for the first iteration, _results[0] having the number of values I expect, however the second iteration causes _results[0] to be the same as _results[1], and the third iteration causes _results[0] and _results[1] to be the same as _results[2]. It works okay when I write the 3 queries separately, however I'd prefer to do it this way.
Is there something I missed about LINQ and datatables? Thanks in advance.
Edit 1: A few comments suggested that sheet was being captured as a constant, however, I don't see how this could be as the variable was being incremented each iteration, but the results of previous iterations are being overwritten by the latest iteration. Here is a link to an imgur album showing the watch window after each iteration.