如何在DataSet中合并两列? [英] How do I merge two columns in a DataSet?

查看:262
本文介绍了如何在DataSet中合并两列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我已经询问过将列插入数据集。我现在有一个类似的问题,即将两列或更多列合并成一列。



让我说我有以下数据集:

  DataSet ds = new DataSet(); 
ds.Tables.Add(new DataTable());
ds.Tables [0] .Columns.Add(id,typeof(int));
ds.Tables [0] .Columns.Add(firstname,typeof(string));
ds.Tables [0] .Columns.Add(lastname,typeof(string));

我需要将firstname和lastname列组合成一个名为name 。



最好是为我创建一个将两列合并在一起的方法,还是可以将多个列合并在一起的更一般的方法?



我的想法是创建一个广义的方法,如下所示:



MergeColumns(string format,string mergedColumn,DataTable dt,params string [] columnsToMerge)



用户提供格式如下:{0} {1}



mergedColumn是新列的名称...但它必须能够与要合并的列中的一个相同,因为在我现实世界的情况下,我合并名称和given_names进入名称...但是如果我需要将街道,城市,状态邮政编码等合并到一个名为地址的列中,我仍然希望能够使用它。



我以为这样使用的方式如下:



MergeColumns({0} {1},name,dataTable,firstname,lastname);



给出上述数据集,我希望结果数据集如下所示:

  DataSet ds = new DataSet(); 
ds.Tables.Add(new DataTable());
ds.Tables [0] .Columns.Add(id,typeof(int));
ds.Tables [0] .Columns.Add(name,typeof(string));

这似乎是一个合理的方法吗?这样的方法是否已经存在?我不想重塑轮子。此外,我创建的方法比现在实际需要的还要多吗?

解决方案

在我看来,这应该会发生在数据层而不是逻辑层。可能数据集消费者知道它需要哪些数据,并且可以明确地从数据存储中请求数据,而无需操纵数据集。在数据库场景中,我基本上是宣称一个多个存储过程方法和数据集消费者将从适当的方法中检索数据。


Previously I've asked about inserting a column into a dataset. I now have a similar question...namely merging two or more columns into a single column.

Lets say I have the following data set:

DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add("id", typeof(int));
ds.Tables[0].Columns.Add("firstname", typeof(string));
ds.Tables[0].Columns.Add("lastname", typeof(string));

I am needing to combine the "firstname" and "lastname" columns into a single column called "name".

Would it be best for me to create a method that merges two columns together or a more general one that can be used to merge multiple columns together?

My idea is to create a generalized method sort of like the following:

MergeColumns(string format, string mergedColumn, DataTable dt, params string[] columnsToMerge)

The user supplies a format as follows: "{0} {1}"

mergedColumn is the name of the new column...but it must be able to be the same as one of the columns that will be merged cause in my real world case I'm merging "name" and "given_names" into "name"...but I still want to be able to use it if I ever needed to merge "street", "city", "state" "postcode" etc into a column called "address".

The way I am thinking this would be used is as follows:

MergeColumns("{0} {1}", "name", dataTable, "firstname", "lastname");

given the above dataset, I would expect the resultant data set to look as follows:

DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add("id", typeof(int));
ds.Tables[0].Columns.Add("name", typeof(string));

Does this seem like a reasonable approach? Does a method like this already exist? I don't want to reinvent the wheel. Also, am I creating a method that does more than I actually need right now?

解决方案

it seems to me that this should happen in the data layer and not the logic layer. presumably the dataset consumer knows what data it needs and could request it from the datastore explicitly, without having to manipulate datasets.

in a database scenario, i'm essentially proponing a multiple stored procedure approach and the dataset consumer would retrieve the data from the appropriate one.

这篇关于如何在DataSet中合并两列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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