示例#1
0
        public static NTuples[] CrossDataRows(DataRow[][] possibleRows, NTuples[] possibleRowsTuple = null)
        {
            if (possibleRowsTuple == null)
            {
                return(possibleRows.Select(r => { var tup = new NTuples(1); tup.AddToTuple(r); return tup; }).ToArray());
            }
            else
            {
                var newPossibleRowTuple = new List <NTuples>();

                foreach (var rows in possibleRows)
                {
                    foreach (var rowTuple in possibleRowsTuple)
                    {
                        var newRowTuple = new NTuples(rowTuple.TupleSize + 1);
                        foreach (var tup in rowTuple.Tuple)
                        {
                            newRowTuple.AddToTuple(tup);
                        }
                        newRowTuple.AddToTuple(rows);
                        newPossibleRowTuple.Add(newRowTuple);
                    }
                }
                return(newPossibleRowTuple.ToArray());
            }
        }
示例#2
0
        public static DataTable CrossTable(DataTable table1, DataTable table2 = null)
        {
            if (table2 == null)
            {
                return(table1);
            }
            if (table1.TableName == table2.TableName)
            {
                table2.TableName += "I";
            }

            var product = new DataTable($"{table1.TableName}_X_{table2.TableName}");
            var tupList = new List <NTuples>();

            foreach (var table in new[] { table1, table2 })
            {
                foreach (DataColumn col in table.Columns)
                {
                    DataColumn newCol = new DataColumn($"{table.TableName}.{col.ColumnName}", col.DataType);
                    product.Columns.Add(newCol);
                }
            }

            foreach (DataRow table1Row in table1.Rows)
            {
                foreach (DataRow table2Row in table2.Rows)
                {
                    var tup = new NTuples(2);
                    tup.AddToTuple(table1Row);
                    tup.AddToTuple(table2Row);
                    tupList.Add(tup);
                }
            }

            foreach (var tup in tupList)
            {
                DataRow newRow = product.NewRow();
                foreach (DataRow ele in tup.Tuple)
                {
                    foreach (DataColumn col in ele.Table.Columns)
                    {
                        newRow[$"{ele.Table.TableName}.{col.ColumnName}"] = ele[col.ColumnName];
                    }
                }
                product.Rows.Add(newRow);
            }

            return(product);
        }