Boxing/unboxing in DataTables and LINQ
Let's say I've got memory-based datatable like this:
DataTable dt = new DataTable();
dt.Columns.Add("c1", System开发者_JS百科.Type.GetType("System.Double"));
dt.Columns.Add("c2", System.Type.GetType("System.Double"));
...
DataRow row = dt.AddRow();
row["c1"] = 1;
row["c2"] = 2;
...
Then I query this table:
List<DataRow> rows = (from r in table where (double)r["c1"] < 2.0 select r).ToList();
And in profiler I see that it creates a lot of doubles. I assume that it somehow related to the comparison.
Any ideas how to get rid of this needless memory allocation?
I use .NET 4, VS 2010, C#.
The underlying storage here is in a typed array (in this case in the DoubleStorage
class), but all access goes via object
, since there is no generic API. You cannot avoid the boxing unless you switch to a class-based model without DataTable
.
精彩评论