views:

3672

answers:

5

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?

+1  A: 

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.

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.
Soviut
yeah, those are valid concerns...which makes me think I should just do this in the simplest way possible.
mezoid
+1  A: 

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.

Jason
actually that's not a bad idea...I just did a double check of the spec I've been given and there is nothing in it that says this can't be done at the stored procedure level...that way I only ever get back a "name" column...
mezoid
I'm voting this as the answer since it allowed me to get the work done easily at the stored proc level. While this answer doesn't answer how to actually merge two columns, it does show that I was over thinking the solution.
mezoid
A: 
        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"]);
Michael Buen
+1  A: 
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() );
    }


}
Michael Buen
this is closer to what I was looking for...so I'm giving it an upvote. I've accepted Jason's answer as it was the simplest solution for my problem. Using the method I was originally thinking I would have had to do too much for a limited benefit. But thanks for your contributions!
mezoid
A: 

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


    }
Michael Buen