C# 如何合并数据集中的两列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/456568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I merge two columns in a DataSet?
提问by mezoid
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".
我需要将“firstname”和“lastname”列合并到一个名为“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)
MergeColumns(字符串格式,字符串mergedColumn,DataTable dt,params string[] columnsToMerge)
The user supplies a format as follows: "{0} {1}"
用户提供如下格式:“{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".
mergedColumn 是新列的名称......但它必须能够与将被合并的列之一相同,因为在我的现实世界中,我将“name”和“given_names”合并为“name” ...但如果我需要将“街道”、“城市”、“州”、“邮政编码”等合并到名为“地址”的列中,我仍然希望能够使用它。
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?
这看起来是一个合理的方法吗?是否已经存在这样的方法?我不想重新发明轮子。另外,我是否正在创建一种比我现在实际需要的更多的方法?
采纳答案by Jason
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.
在数据库场景中,我本质上是在提出一种多存储过程方法,数据集使用者将从适当的方法中检索数据。
回答by Soviut
If the dataset is specific enough, a MergeName() method is fine. This is especially true if you don't think you'll be using it in more than once place. A generic method will require considerably more work, but could be worth it if you're doing a lot of merging.
如果数据集足够具体,则 MergeName() 方法就可以了。如果您认为不会多次使用它,则尤其如此。通用方法将需要更多的工作,但如果您进行大量合并,则可能值得。
Some things I can think of off the top of my head that you'll have to deal with:
我可以想到一些你必须处理的事情:
- One issue you may run into is type checking; what happens if a user wants to merge a date field or a numeric field.
- What happens to the old fields? Will they remain or be deleted? If you're using this as a direct data source, you may want to remove left over columns, so some kind of
RemoveOldColumns
flag may be a good idea.
- 您可能会遇到的一个问题是类型检查;如果用户想要合并日期字段或数字字段会发生什么。
- 旧田怎么办?它们会保留还是被删除?如果您将此用作直接数据源,您可能希望删除剩余的列,因此某种
RemoveOldColumns
标志可能是个好主意。
回答by Michael Buen
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));
ds.Tables[0].Rows.Add(1,"torvalds", "linus");
ds.Tables[0].Rows.Add(2,"lennon", "john");
ds.Tables[0].Columns.Add("name", typeof(string));
foreach (DataRow dr in ds.Tables[0].Rows) dr["name"] = dr["lastname"] + ", " + dr["firstname"];
foreach (DataRow dr in ds.Tables[0].Rows)
MessageBox.Show(dr["name"]);
回答by Michael Buen
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
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));
ds.Tables[0].Rows.Add(1,"torvalds", "linus");
ds.Tables[0].Rows.Add(2,"lennon", "john");
ds.Tables[0].MergeColumns("name", "lastname", "firstname");
foreach (DataRow dr in ds.Tables[0].Rows)
MessageBox.Show(dr["name"].ToString());
}
}
public static class Helper
{
public static void MergeColumns(this DataTable t, string newColumn, params string[] columnsToMerge)
{
t.Columns.Add(newColumn, typeof(string));
var sb = new StringBuilder();
sb.Append("{0}, ");
for (int i = 1; i < columnsToMerge.Length; ++i)
sb.Append("{" + i.ToString() + "}");
string format = sb.ToString();
foreach(DataRow r in t.Rows)
r[newColumn] = string.Format(format, columnsToMerge.Select(col => r[col]).ToArray() );
}
}
回答by Michael Buen
alternatively, you can do this:
或者,您可以这样做:
public Form1()
{
InitializeComponent();
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));
ds.Tables[0].Rows.Add(1,"torvalds", "linus");
ds.Tables[0].Rows.Add(2,"lennon", "john");
ds.Tables[0].Columns.Add("name", typeof(string), "lastname + ', ' + firstname");
foreach (DataRow dr in ds.Tables[0].Rows)
MessageBox.Show(dr["name"].ToString());
}
回答by Adi
This Exactly was my requirement and after a lot of searching I ended up in deciding that its better to create your own datatable and use that as your datasource to the ListBox or DropDownList whichever is required.
这正是我的要求,经过大量搜索,我最终决定创建自己的数据表并将其用作 ListBox 或 DropDownList 所需的数据源更好。
DataTable dt = new DataTable();
dt.Columns.Add("Test",typeof(String));
foreach (DataRow r in ds.Tables[0].Rows)
{
DataRow dr = dt.NewRow();
dr["Test"] = r["LastName"] + " ," + r["firstName"];
dt.Rows.Add(dr);
}
Here ds
is the dataset containing the main return table of the procedure.
这ds
是包含程序主要返回表的数据集。
回答by Himanshu Shukla
string result = string.Join(",", dtTotalQuery.AsEnumerable().Select(row => dtTotalQuery.Rows[0].ItemArray[0] + " " + dtTotalQuery.Rows[0].ItemArray[1]));
回答by Sameera R.
Extension Method With selectable seperatorand position,
扩展方法 可选择分隔符和位置,
public static class Helper
{
public static void MergeColumns(this DataTable t, string newColumn, params string[] columnsToMerge)
{
t.Columns.Add(newColumn, typeof(string));
var sb = new StringBuilder();
sb.Append("{0}, ");
for (int i = 1; i < columnsToMerge.Length; ++i)
sb.Append("{" + i.ToString() + "}");
string format = sb.ToString();
foreach(DataRow r in t.Rows)
r[newColumn] = string.Format(format, columnsToMerge.Select(col => r[col]).ToArray() );
}
}
Usage
用法
//DataTable dt
dt.MergeColumns("name", 0, " ", "lastname", "firstname");