Ejemplo n.º 1
0
        public DataTable ToTable(string tablename, bool isDistinct, params string[] columnNames)
        {
            if (columnNames == null)
            {
                throw new ArgumentNullException("columnNames", "'columnNames' argument cannot be null.");
            }

            DataTable newTable = new DataTable(tablename);

            DataColumn[]        columns;
            ListSortDirection[] sortDirection = null;
            if (columnNames.Length != 0)
            {
                columns = new DataColumn [columnNames.Length];
                for (int i = 0; i < columnNames.Length; ++i)
                {
                    columns [i] = Table.Columns [columnNames [i]];
                }

                if (sortColumns != null)
                {
                    sortDirection = new ListSortDirection [columnNames.Length];
                    for (int i = 0; i < columnNames.Length; ++i)
                    {
                        sortDirection [i] = ListSortDirection.Ascending;
                        for (int j = 0; j < sortColumns.Length; ++j)
                        {
                            if (sortColumns [j] != columns [i])
                            {
                                continue;
                            }
                            sortDirection [i] = sortOrder [j];
                        }
                    }
                }
            }
            else
            {
                columns       = (DataColumn[])Table.Columns.ToArray(typeof(DataColumn));
                sortDirection = sortOrder;
            }

            ArrayList expressionCols = new ArrayList();

            for (int i = 0; i < columns.Length; ++i)
            {
                DataColumn col = columns [i].Clone();
                if (col.Expression != String.Empty)
                {
                    col.Expression = "";
                    expressionCols.Add(col);
                }
                if (col.ReadOnly)
                {
                    col.ReadOnly = false;
                }
                newTable.Columns.Add(col);
            }

            DataRow [] rows;
            Index      index = new Index(new Key(Table, columns, sortDirection, RowStateFilter, rowFilterExpr));

            if (isDistinct)
            {
                rows = index.GetDistinctRows();
            }
            else
            {
                rows = index.GetAllRows();
            }

            foreach (DataRow row in rows)
            {
                DataRow newRow = newTable.NewNotInitializedRow();
                newTable.Rows.AddInternal(newRow);
                newRow.Original = -1;
                if (row.HasVersion(DataRowVersion.Current))
                {
                    newRow.Current = newTable.RecordCache.CopyRecord(Table, row.Current, -1);
                }
                else if (row.HasVersion(DataRowVersion.Original))
                {
                    newRow.Current = newTable.RecordCache.CopyRecord(Table, row.Original, -1);
                }

                foreach (DataColumn col in expressionCols)
                {
                    newRow [col] = row [col.ColumnName];
                }
                newRow.Original = -1;
            }
            return(newTable);
        }