将两个数据集合并为一个 [英] Combine two datasets into one

查看:514
本文介绍了将两个数据集合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将两个不同的数据集组合成一个吗?



哪个没有任何公共行

解决方案

您可以将两个数据集组合成一个数据集,无论它们是否具有公共行和/或不同/匹配列。这取决于架构定义的设置方式(主键?)以及 MissingSchemaAction [ ^ ]值是当一个人调用 DataSet.Merge(DataSet,Boolean,MissingSchemaAction) [ ^ ]方法。



问候,

- Manfred


你的问题不清楚你是否真的需要结合两个数据集(即一组表)或简单地两个数据表(行集)。

在这两种情况下,答案是:是的,当然,因为你总能建立一个结果集,即输入的联合(例如在datatables的情况下,你构建一个结果数据表,其中包含两个输入数据表的所有列和行。

问题是:这样的''combine''操作是否会产生有意义的结果?

没有简单的解决方案 - 你要做的是一个复杂的过程,即使是非常小的数据样本。

例如:

 DataTable dt1 =  new  DataTable(); 
dt1.Columns.Add( ID);
dt1.Columns.Add( 名称);
dt1.Rows.Add( 1 佛瑞德);
dt1.Rows.Add( 2 标记);
dt1.Rows.Add( 3 米歇尔);
DataTable dt2 = new DataTable();
dt2.Columns.Add( ID);
dt2.Columns.Add( 年龄);
dt2.Rows.Add( 1 23\" );
dt2.Rows.Add( 2 23\" );
dt2.Rows.Add( 3 25\" );

var result = 来自 d1 in dt1.AsEnumerable()
join d2 in dt2.AsEnumerable( )在d1 [ ID]等于d2 [ ID]
选择
{
Id = d1 [ ID] ,
名称= d1 [ 名称],
年龄= d2 [ 年龄]
};
DataTable dt3 = result.ToList()。ToDataTable();

创建两个(相关的)数据表,并使用Linq和扩展方法生成组合表。

(扩展方法可在此处获取:将列表转换为DataTable [ ^ ])



但这取决于具有相关信息的两个表。当您处理可能在每个表中有多个表的单独DataSet时,它会变得更加复杂。



如果可能,最好在数据上执行此操作source - 例如您的数据库。


Can I combine two different datasets into a single one?

Which do not have any common rows

解决方案

You can most certainly combine two datasets into one dataset regardless if they have common rows and/or disparate/matching columns. It depends on how the schema definition is set up (primary key?)and what the MissingSchemaAction[^] value is when one calls the DataSet.Merge(DataSet, Boolean, MissingSchemaAction)[^] method.

Regards,
— Manfred


From your question is not clear if you really need to combine two datasets (that is set of tables) or simply two datatables (set of rows).
In both cases the answer is: "yes, of course", because you can always build a resulting set, namely the union of the input ones (e.g. in datatables case, you build a resulting datatable having all the columns and rows of both the input datatables).
The problem is: should such ''combine'' operation produce a meaningful result?


There is no "simple solution" - what you are trying to do is a complex process, even with pretty small data samples.
For example:

DataTable dt1 = new DataTable();
dt1.Columns.Add("ID");
dt1.Columns.Add("Name");
dt1.Rows.Add(1, "Fred");
dt1.Rows.Add(2, "Mark");
dt1.Rows.Add(3, "Michelle");
DataTable dt2 = new DataTable();
dt2.Columns.Add("ID");
dt2.Columns.Add("Age");
dt2.Rows.Add(1, "23");
dt2.Rows.Add(2, "23");
dt2.Rows.Add(3, "25");

var result = from d1 in dt1.AsEnumerable()
             join d2 in dt2.AsEnumerable() on d1["ID"] equals d2["ID"]
             select new
                {
                    Id = d1["ID"],
                    Name = d1["Name"],
                    Age = d2["Age"]
                };
DataTable dt3 = result.ToList().ToDataTable();

Creates two (related) data tables, and uses Linq and an extension method to generate a combined table.
(The extension method is available here: Converting a List to a DataTable[^])

But this is dependant on the two tables having related information. When you are dealing with separate DataSets which will likely have multiple tables in each, it becomes even more complicated.

If possible it would be better to do this at the data source - your database for example.


这篇关于将两个数据集合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆