我有一个数据源,我不想绑定X的集合。 X应包含特定类型Y的属性的子集(假设Y具有属性PropOne,PropTwo,PropThree)这当然可以使用匿名类型完成:
void DoBind() { myGrid.DataSource = myCollectionOfYs.Select(y => new {y.PropOne, y.PropTwo}); }如何修改此方法,以便它的调用者可以以类型安全的方式指定在投影中使用哪些属性? 也就是说:
var expressions = new List<Expression<Func<Y, object>>>(); expressions.Add(y => y.PropOne); expressions.Add(y => y.PropTwo); DoBind(expressions);I have a data source to which I wan't to bind a collection of X's. X should contain a subset of a certain type Y's properties (let's say Y has properties PropOne, PropTwo, PropThree) This can of course be done with an anonymous type:
void DoBind() { myGrid.DataSource = myCollectionOfYs.Select(y => new {y.PropOne, y.PropTwo}); }How can I modify this method so that the caller of it can specify which properties to use in the projection in a type safe way? I.e. something along the lines:
var expressions = new List<Expression<Func<Y, object>>>(); expressions.Add(y => y.PropOne); expressions.Add(y => y.PropTwo); DoBind(expressions);最满意答案
用你的想法:
void DoBind(Func<Y, object> func) { myGrid.DataSource = myCollectionOfYs.Select(funct); }并使用如下:
DoBind(y => new {y.PropOne});Using your idea:
void DoBind(Func<Y, object> func) { myGrid.DataSource = myCollectionOfYs.Select(funct); }And use like:
DoBind(y => new {y.PropOne});使用来自另一个实例的属性子集以类型安全的方式创建实例?(Create an instance using a subset of properties from another instance in a type safe manner?)我有一个数据源,我不想绑定X的集合。 X应包含特定类型Y的属性的子集(假设Y具有属性PropOne,PropTwo,PropThree)这当然可以使用匿名类型完成:
void DoBind() { myGrid.DataSource = myCollectionOfYs.Select(y => new {y.PropOne, y.PropTwo}); }如何修改此方法,以便它的调用者可以以类型安全的方式指定在投影中使用哪些属性? 也就是说:
var expressions = new List<Expression<Func<Y, object>>>(); expressions.Add(y => y.PropOne); expressions.Add(y => y.PropTwo); DoBind(expressions);I have a data source to which I wan't to bind a collection of X's. X should contain a subset of a certain type Y's properties (let's say Y has properties PropOne, PropTwo, PropThree) This can of course be done with an anonymous type:
void DoBind() { myGrid.DataSource = myCollectionOfYs.Select(y => new {y.PropOne, y.PropTwo}); }How can I modify this method so that the caller of it can specify which properties to use in the projection in a type safe way? I.e. something along the lines:
var expressions = new List<Expression<Func<Y, object>>>(); expressions.Add(y => y.PropOne); expressions.Add(y => y.PropTwo); DoBind(expressions);最满意答案
用你的想法:
void DoBind(Func<Y, object> func) { myGrid.DataSource = myCollectionOfYs.Select(funct); }并使用如下:
DoBind(y => new {y.PropOne});Using your idea:
void DoBind(Func<Y, object> func) { myGrid.DataSource = myCollectionOfYs.Select(funct); }And use like:
DoBind(y => new {y.PropOne});
发布评论