views:

369

answers:

3

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

+1  A: 

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...

CMS
CMS, do you see any problem with adding the underscore or some other character and replacing this character with a space within the user control caption?
SaaS Developer
A: 

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.

ICR
You can't specify attributes in anonymous object creation expressions.
Jon Skeet
+3  A: 

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.

tvanfosson