开发者

Subsonic and Two DataText Columns in ASP.net Dropdownlist control

开发者 https://www.devze.com 2023-01-06 16:59 出处:网络
Does anyone know how I can concatenate two开发者_JAVA百科 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

Does anyone know how I can concatenate two开发者_JAVA百科 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!


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 )


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消