Open yellis opened 10 years ago
MutableDataTable is an array of column arrays. This makes it very easy to rearrange columns, but it means adding a new row is expensive since it means reallocating every column.
The approach I’ve used for row-based operations has been
Using a 2-d dictionary (this is also great for sparse tables):
static void Main(string[] args)
{
var d = new Dictionary2d<int,string,string>();
d[0,"Name"] = "jack";
d[0, "score"] = "23";
d[1,"Name"] = "jill";
d[1, "score"] = "45";
var table = DataTable.New.From2dDictionary(d);
table.SaveToStream(Console.Out);
}
Using an IEnumerable: ''' class Row { public string Name { get; set; } public int score { get; set; } }
static IEnumerable<Row> GetRows()
{
yield return new Row { Name = "Jack", score = 23 };
yield return new Row { Name = "Jill", score = 45 };
}
static void Main(string[] args)
{
var table = DataTable.New.FromEnumerable(GetRows());
table.SaveToStream(Console.Out);
}
'''
Cells in a datatables can be changed like so:
''' // Mutate table.Columns[0].Values[1] = "Jen"; '''
DataTable.New
contains different methods of creating a newMutableDataTable
based on different source data. However, it appears that there is no way right now to add data one row at a time to aMutableDataTable
, nor is there an easy way to modify specific "cells" of data in a table.What I am looking for (adding a few different possible approaches):