Mark Mark - 9 months ago 55
C# Question

Data structure similar to data.frames in R

I want to have a flexible data structure in C# that can work like dataframes in R and Python (or like a excel sheet).

The closest I could find was the native data table in C#.

Is there a better table structure that can hold data from different types in different columns?

Answer Source

You could use a DataTable in .NET. It will allow you to define a set of typed columns (see the Columns property) as well as add a number of rows (see the Rows property).

Once you have that, you can easily access any row by:

However, consider the following: A List<T> of anonymous types (from a projection from a query in LINQ) or strong types is probably just as good, if not better (no need for strings to get columns) depending on your need (if you need dynamic access dictated from user input, then using a DataTable would be better).

If you simply want a cross matrix of values, an anonymous type is an easy way to get a strongly-typed, compile-time-checked list of values, which has many more benefits than dynamically looking up the values.