【发布时间】:2014-09-23 14:35:23
【问题描述】:
是否有可能创建一个具有 2 个输出值的函数?
我需要一种从对象中获取 2 个属性的方法,最好是通过表达式,因为 易于使用。 将它与查询提供程序一起使用来选择多个字段。
protected Expression<Func<T, TProperty1, TProperty2>> Select2Properties { get; set; }
public MyClass(Expression<Func<T, TProperty1, TProperty2>> selector) {
Select2Properties = selector;
}
// desired usage (pseudo)
x => (x.Property1, x.Property);
我知道这完全是垃圾,但任何其他解决方案(例如,需要 2 个表达式属性选择器或需要一个元组)都会导致有时无法读取构造函数调用,尤其是当我需要超过 2 个属性时:
x => x.Property1, x => x.Property2, x => x.Property3, x => x.Property4
// or
x => Tuple.Create(x.Property1, x.Property2, x.Property3, x.Property4)
有没有办法实现我想要的?
【问题讨论】:
标签: c# lambda expression func