如何合并数据集中的两列?

How do I merge two columns in a DataSet?(如何合并数据集中的两列?)

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

问题描述

之前我问过 在数据集中插入一列.我现在有一个类似的问题...即将两列或多列合并为一列.

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.

假设我有以下数据集:

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"的列.

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(字符串格式,字符串mergeColumn,DataTable dt,params string[] columnsToMerge)

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

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

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

mergedColumn 是新列的名称......但它必须能够与将要合并的列之一相同,因为在我的现实世界案例中,我将name"和given_names"合并到name"...但如果我需要将street"、city"、state"、postcode"等合并到一个名为address"的列中,我仍然希望能够使用它.

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");

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.

这篇关于如何合并数据集中的两列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何合并数据集中的两列?

基础教程推荐