47682be96a51e45bddb35530547789ee326f92ac
[mono.git] / mcs / class / System.Data.DataSetExtensions / System.Data / DataTableExtensions.cs
1 //
2 // DataTableExtensions.cs
3 //
4 // Author:
5 //   Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc. http://www.novell.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Linq;
35
36
37 namespace System.Data
38 {
39
40         public static class DataTableExtensions
41         {
42                 public static DataView AsDataView (this DataTable table)
43                 {
44                         return AsDataView<DataRow> (table.AsEnumerable ());
45                 }
46
47                 [MonoTODO ("We should implement an effective DataView derivation; looks like .NET does.")]
48                 public static DataView AsDataView<T> (this EnumerableRowCollection<T> source)
49                         where T : DataRow
50                 {
51                         return CopyToDataTable<T> (source).DefaultView;
52                 }
53
54                 public static EnumerableRowCollection<DataRow> AsEnumerable (this DataTable source)
55                 {
56                         return new EnumerableRowCollection<DataRow> (new DataRowEnumerable<DataRow> (source));
57                 }
58
59                 public static DataTable CopyToDataTable<T> (this IEnumerable<T> source)
60                         where T : DataRow
61                 {
62                         DataTable dt = new DataTable ();
63                         IEnumerator<T> e = source.GetEnumerator ();
64                         if (!e.MoveNext ())
65                                 throw new InvalidOperationException ("The source contains no DataRows");
66                         foreach (DataColumn col in e.Current.Table.Columns)
67                                 dt.Columns.Add (new DataColumn (col.ColumnName, col.DataType, col.Expression, col.ColumnMapping));
68                         CopyToDataTable<T> (source, dt, LoadOption.PreserveChanges);
69                         return dt;
70                 }
71
72                 public static void CopyToDataTable<T> (this IEnumerable<T> source, DataTable table, LoadOption options)
73                         where T : DataRow
74                 {
75                         CopyToDataTable<T> (source, table, options, null);
76                 }
77
78                 public static void CopyToDataTable<T> (this IEnumerable<T> source, DataTable table, LoadOption options, FillErrorEventHandler errorHandler)
79                         where T : DataRow
80                 {
81                         var reader = new RowEnumerableDataReader (source, 0);
82                         table.Load (reader, options, errorHandler);
83                 }
84         }
85
86         class DataRowEnumerable<TRow> : IEnumerable<TRow>
87         {
88                 DataTable source;
89
90                 public DataRowEnumerable (DataTable source)
91                 {
92                         this.source = source;
93                 }
94
95                 public IEnumerator<TRow> GetEnumerator ()
96                 {
97                         foreach (TRow row in source.Rows)
98                                 yield return row;
99                 }
100
101                 IEnumerator IEnumerable.GetEnumerator ()
102                 {
103                         return GetEnumerator ();
104                 }
105         }
106 }