SaaS Developer
SaaS Developer

Reputation: 9895

C# 3.0 Anonymous Types: Naming

I was wondering if there is some way to name or rename a property on an Anonymous type to include a space in the property name. For example:

var resultSet = from customer in customerList
    select new 
    {
       FirstName = customer.firstName;
    };

In this example I would like FirstName to be "First Name". The reason for this question, is I have a user control that exposes a public DataSource property that I bind to different anonymous type. It is working perfectly right now, except for the one little shortcoming of the column names being a little less than user friendly (FirstName instead of First Name).

Upvotes: 3

Views: 2553

Answers (3)

tvanfosson
tvanfosson

Reputation: 532455

What about doing something like this:

var resultSet = from customer in customerList
                select new 
                {
                    Value = customer.firstName,
                    Title = "First Name"
                };

Then in your user control use Value as the contents and Title as the column name.

Upvotes: 6

ICR
ICR

Reputation: 14162

I'd add an attribute to the property where you can specify a custom name, and you can provide more user friendly names using the attribute.

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827366

No, its not possible, spaces are not allowed in the names of members, you can use perhaps underscore or programmatically change the captions of your columns after the data is bound...

Upvotes: 3

Related Questions