importing messaging-2008 branch to trunk, going on.
[mono.git] / mcs / class / System.Data.DataSetExtensions / System.Data / DataTableExtensions.cs
index 3ff7e9fc88169c5dc8854af8ad4b878b20b48c74..47682be96a51e45bddb35530547789ee326f92ac 100644 (file)
@@ -29,6 +29,7 @@
 //
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
@@ -38,45 +39,68 @@ namespace System.Data
 
        public static class DataTableExtensions
        {
-               [MonoTODO]
                public static DataView AsDataView (this DataTable table)
                {
-                       return table.DefaultView;
+                       return AsDataView<DataRow> (table.AsEnumerable ());
                }
 
-               [MonoTODO]
+               [MonoTODO ("We should implement an effective DataView derivation; looks like .NET does.")]
                public static DataView AsDataView<T> (this EnumerableRowCollection<T> source)
                        where T : DataRow
                {
-                       throw new NotImplementedException ();
+                       return CopyToDataTable<T> (source).DefaultView;
                }
 
                public static EnumerableRowCollection<DataRow> AsEnumerable (this DataTable source)
                {
-                       return new EnumerableRowCollection<DataRow> (source);
+                       return new EnumerableRowCollection<DataRow> (new DataRowEnumerable<DataRow> (source));
                }
 
-               [MonoTODO]
                public static DataTable CopyToDataTable<T> (this IEnumerable<T> source)
                        where T : DataRow
                {
                        DataTable dt = new DataTable ();
+                       IEnumerator<T> e = source.GetEnumerator ();
+                       if (!e.MoveNext ())
+                               throw new InvalidOperationException ("The source contains no DataRows");
+                       foreach (DataColumn col in e.Current.Table.Columns)
+                               dt.Columns.Add (new DataColumn (col.ColumnName, col.DataType, col.Expression, col.ColumnMapping));
                        CopyToDataTable<T> (source, dt, LoadOption.PreserveChanges);
                        return dt;
                }
 
-               [MonoTODO]
                public static void CopyToDataTable<T> (this IEnumerable<T> source, DataTable table, LoadOption options)
                        where T : DataRow
                {
                        CopyToDataTable<T> (source, table, options, null);
                }
 
-               [MonoTODO]
                public static void CopyToDataTable<T> (this IEnumerable<T> source, DataTable table, LoadOption options, FillErrorEventHandler errorHandler)
                        where T : DataRow
                {
-                       throw new NotImplementedException ();
+                       var reader = new RowEnumerableDataReader (source, 0);
+                       table.Load (reader, options, errorHandler);
+               }
+       }
+
+       class DataRowEnumerable<TRow> : IEnumerable<TRow>
+       {
+               DataTable source;
+
+               public DataRowEnumerable (DataTable source)
+               {
+                       this.source = source;
+               }
+
+               public IEnumerator<TRow> GetEnumerator ()
+               {
+                       foreach (TRow row in source.Rows)
+                               yield return row;
+               }
+
+               IEnumerator IEnumerable.GetEnumerator ()
+               {
+                       return GetEnumerator ();
                }
        }
 }