open System open System.Data module DataTableMaker = type ColumnMaker<'t> = { Name: string; Type: Type; GetValue: 't -> obj } let makeColumn name (f: _ -> 'c) = { Name = name; Type = typeof<'c>; GetValue = f >> box } let makeDataTable items (columns : #_ seq)= let table = new DataTable() let addColumn colName colType = table.Columns.Add(colName, colType) |> ignore let addRow (data: obj seq) = Seq.toArray data |> table.Rows.Add |> ignore for c in columns do addColumn c.Name c.Type for i in items do columns |> Seq.map (fun c-> c.GetValue i) |> addRow table open DataTableMaker open System.Runtime.CompilerServices // make this stuff easy to call from C#/VB.NET with helper and extension methods type ColumnMaker = static member Make(columnName, getValue: Func<'t, 'c>) = { Name = columnName; Type = typeof<'c>; GetValue = getValue.Invoke >> box } [] module EnumerableExtensions = /// create a from a sequence /// of items () and column descriptors () /// the values from wich corresponding rows () will be created /// [] let MakeDataTable(items, [] columns) = DataTableMaker.makeDataTable items columns (* how to call: let foo = [(1,2);(3,4)] let t = DataTableMaker.makeDataTable foo [|makeColumn "a" fst|] *)