My expression aren't great and I would like to improve on them so I am wondering if someone could explain for me if it possible to create a property in a class that can be given a value during instantiation like this:
new Column<Product>{ ColumnProperty = p => p.Title};
or better still (but I think I am pushing it)
new Column {ColumnProperty = p => p.Title};
with a class something like this:
public class Column<TModel>
{
public Expression<Func<TModel, TProperty>> ColumnProperty { get; set; }
}
The basic idea behind it is that I create a Grid from a bunch of Column objects something like this.
List<Product> productList = GetProductsFromDb();
List<Column> columnList = new List<Column>();
columnList.Add(new Column<Product> {ColumnProperty = p => p.ProductId, Heading = "Product Id"});
columnList.Add(new Column<Product> {ColumnProperty = p => p.Title, Heading = "Title"});
Grid myGrid = new Grid(productList, columnList);
This may not be the tidiest/easiest way to do this but I am interested in knowing whether it can be done as I want to improve my understanding of expressions and I love being able to have strongly typed values instead of string literals it's so much nicer to work with.
Any thoughts, ideas, ramblings would be greatly appreciated
Cheers Rob