C# 关联两个dataTable 比如 都有1个id 列 用此id关联,不要用循环 数据量很大 执行起来可能很慢!
2个回答
展开全部
用DataSet 做 join
可以参考;
public static DataTable Join (DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC)
{
//创建一个新的DataTable
DataTable table = new DataTable("Join");
// Use a DataSet to leverage DataRelation
using(DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.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 r = new DataRelation(string.Empty,parentcolumns,childcolumns,false);
ds.Relations.Add(r);
//为关联表创建列
for(int i = 0; i < First.Columns.Count; i )
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for(int i = 0; i < Second.Columns.Count; i )
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if(!table.Columns.Contains(Second.Columns[i].ColumnName))
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
else
table.Columns.Add(Second.Columns[i].ColumnName "_Second", Second.Columns[i].DataType);
}
table.BeginLoadData();
foreach(DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
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;
}
public static DataTable Join (DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
{
return Join(First, Second, new DataColumn[]{FJC}, new DataColumn[]{SJC});
}
public static DataTable Join (DataTable First, DataTable Second, string FJC, string SJC)
{
return Join(First, Second, new DataColumn[]{First.Columns[FJC]}, new DataColumn[]{First.Columns[SJC]});
}
}
}
可以参考;
public static DataTable Join (DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC)
{
//创建一个新的DataTable
DataTable table = new DataTable("Join");
// Use a DataSet to leverage DataRelation
using(DataSet ds = new DataSet())
{
//把DataTable Copy到DataSet中
ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.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 r = new DataRelation(string.Empty,parentcolumns,childcolumns,false);
ds.Relations.Add(r);
//为关联表创建列
for(int i = 0; i < First.Columns.Count; i )
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
for(int i = 0; i < Second.Columns.Count; i )
{
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
if(!table.Columns.Contains(Second.Columns[i].ColumnName))
table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
else
table.Columns.Add(Second.Columns[i].ColumnName "_Second", Second.Columns[i].DataType);
}
table.BeginLoadData();
foreach(DataRow firstrow in ds.Tables[0].Rows)
{
//得到行的数据
DataRow[] childrows = firstrow.GetChildRows(r);
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;
}
public static DataTable Join (DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
{
return Join(First, Second, new DataColumn[]{FJC}, new DataColumn[]{SJC});
}
public static DataTable Join (DataTable First, DataTable Second, string FJC, string SJC)
{
return Join(First, Second, new DataColumn[]{First.Columns[FJC]}, new DataColumn[]{First.Columns[SJC]});
}
}
}
追问
能否再详细点,这段代码,一下子理解不了
追答
这个应该算是封装好了。
你传入两个 dataTable,和两个 table 关联的 cloumn,返回的就是关联后的结果。
注释应该虽然简明扼要,但是也不难理解。
再想深入理解,可以查看 DataRelation 相关说明。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询