如何合并DataSet中的两列?

问题描述 投票:2回答:8

以前我问过inserting a column into a 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(字符串格式,字符串mergedColumn,DataTable dt,params string [] columnsToMerge)

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

mergedColumn是新列的名称...但它必须能够与将要合并的列之一相同,因为在我的真实案例中我将“name”和“given_names”合并为“name” ...但是如果我需要将“街道”,“城市”,“州”,“邮政编码”等合并到名为“地址”的列中,我仍然希望能够使用它。

我认为这将使用的方式如下:

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

这看起来像是一种合理的方法吗?这样的方法是否已经存在?我不想重新发明轮子。另外,我是否正在创建一种比我现在实际需要的更多的方法?

c# merge dataset
8个回答
1
投票

在我看来,这应该发生在数据层而不是逻辑层。可能数据集消费者知道它需要什么数据,并且可以明确地从数据存储区请求它,而不必操纵数据集。

在数据库场景中,我实际上是在推销多存储过程方法,而数据集使用者将从适当的方法中检索数据。


5
投票
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() );
    }


}

1
投票

如果数据集足够具体,可以使用MergeName()方法。如果您认为自己不会在多个地方使用它,则尤其如此。通用方法需要更多的工作,但如果你做了很多合并,那么它是值得的。

我能想到的一些你必须要处理的事情:

  • 您可能遇到的一个问题是类型检查;如果用户想要合并日期字段或数字字段会发生什么。
  • 旧油田会发生什么?它们会保留还是被删除?如果您将其用作直接数据源,则可能需要删除左侧列,因此某种RemoveOldColumns标志可能是个好主意。

1
投票

或者,您可以这样做:

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


    }

0
投票
        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"]);

0
投票

这完全是我的要求,经过大量的搜索,我最终决定创建自己的数据表并将其用作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);
}

这里ds是包含该过程的主要返回表的数据集。


0
投票
string result = string.Join(",", dtTotalQuery.AsEnumerable().Select(row => dtTotalQuery.Rows[0].ItemArray[0] + " " + dtTotalQuery.Rows[0].ItemArray[1]));

0
投票

扩展方法可选择分离器和位置,

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() );
    }
}

用法

//DataTable dt
dt.MergeColumns("name", 0, " ", "lastname", "firstname");
© www.soinside.com 2019 - 2024. All rights reserved.