views:

29

answers:

2

Hi,

Does anyone know how I can concatenate two columns in my subsonic datasource? I want to display a first and last name in this dropdownlist but I don't want to put them in the same SQL column.

Thanks in advance!

A: 

As with anything, can be done many different ways.

The first that comes to mind is: You could a new anonymous object in the select and concatenate them there.

Nate Pinchot
A: 

On way (SubSonic 3 only)

var result = (
               from p in products
               select new { Id = p.Id, 
                            DisplayName = p.ProductCode + " " + p.ProductName }
             ).ToList();

Another approach (SubSonic 2)

public class ProductList
{
    public int Id {get;set;}   
    public string Displayname {get;set;}
}

public void Foo()
{

    var result = DB.Select
        (
          Product.Columns.Id,
          "Concat(" + Product.Columns.ProductCode + ", "
                     Product.Columns.ProductName + ") as DisplayName"
        ).From<Products>()
         .ExecuteTypedList<ProductList>();
}

Or with a DataTable

var result = new ProductCollection().Load().ToDataTable();
result.Columns.Add(
      "DisplayName", typeof(string), "ProductCode + ' ' + ProductName"
);

The last parameter is a expression (as defined here: http://msdn.microsoft.com/en-us/library/ms810291.aspx )

SchlaWiener
You are so full of win... I don't know what to do.Thanks very much, that first snippet worked perfectly for me. You're awesome!
Walter