• 2个或多个datable类似于sql inner join 合并查询


     public static DataTable DatableInnerJoin(DataTable FirstTB, DataTable SecondTB, DataColumn[] FJC, DataColumn[] SJC)
        {
            if (FirstTB == null || SecondTB == null)
                return null;
            if (FJC == null || FJC.Length <= 0)
                return null;
            if (SJC == null || SJC.Length <= 0)
                return null;
            DataTable table = new DataTable("Join");
            using (DataSet ds = new DataSet())
            {
                ds.Tables.AddRange(new DataTable[] { FirstTB.Copy(), SecondTB.Copy() });
                DataColumn[] parentcolumns = new DataColumn[FJC.Length];
                for (int i = 0; i < parentcolumns.Length; i++)
                {
                    parentcolumns[i] = ds.Tables[0].Columns[FJC[i].ColumnName];
                }
                DataColumn[] childcolumns = new DataColumn[SJC.Length];
                for (int i = 0; i < childcolumns.Length; i++)
                {
                    childcolumns[i] = ds.Tables[1].Columns[SJC[i].ColumnName];
                }
                DataRelation relation = new DataRelation("Relation_Table", parentcolumns, childcolumns, false);
                ds.Relations.Add(relation);
                for (int i = 0; i < FirstTB.Columns.Count; i++)
                {
                    table.Columns.Add(FirstTB.Columns[i].ColumnName, FirstTB.Columns[i].DataType);
                }
    
                for (int i = 0; i < SecondTB.Columns.Count; i++)
                {
                    if (!table.Columns.Contains(SecondTB.Columns[i].ColumnName))
                        table.Columns.Add(SecondTB.Columns[i].ColumnName, SecondTB.Columns[i].DataType);
                    else
                        table.Columns.Add(SecondTB.Columns[i].ColumnName + "_Second", SecondTB.Columns[i].DataType);
                }
                table.BeginLoadData();
                foreach (DataRow firstrow in ds.Tables[0].Rows)
                {
                    DataRow[] childrows = firstrow.GetChildRows(relation);
                    if (childrows != null && childrows.Length > 0)
                    {
                        object[] parentarray = firstrow.ItemArray;
                        foreach (DataRow secondrow in childrows)
                        {
                            object[] secondarray = secondrow.ItemArray;
                            object[] joinarray = new object[parentarray.Length + secondarray.Length];
                            Array.Copy(parentarray, 0, joinarray, 0, parentarray.Length);
                            Array.Copy(secondarray, 0, joinarray, parentarray.Length, secondarray.Length);
                            table.LoadDataRow(joinarray, true);
                        }
                    }
                }
                table.EndLoadData();
            }
            return table;
        }
  • 相关阅读:
    前端开发--vue开发部分报错指南
    前端开发--Mongodb篇
    前端开发--nginx番外篇
    前端开发--nginx篇
    Mac笔记本使用小道解答集
    vue开发路由相关基础知识和笔记
    PostCSS的插件 -- 关于vue rem适配布局方案
    Nodejs -- 使用koa2搭建数据爬虫
    mac OSX 实用快捷键
    如何将自己的vue组件发布为npm包
  • 原文地址:https://www.cnblogs.com/keyyang/p/6141462.html
Copyright © 2020-2023  润新知