我有以下代码段,但x.PropertyThree的值为零,尽管我无法弄清楚这段代码如何跟踪属性映射的位置。你能帮忙解释一下这里发生了什么吗? x.PropertyThree中的x来自哪里? 谢谢,
代码我正在努力
var data = DoSomething(
model,
options,
x => x.KeyDisplayValue,
(x, s) => new List<object>
{
Magic(x.PropertyOne, s),
Magic(x.PropertyTwo, s),
Magic(x.PropertyThree, s),
x.PropertyFour,
x.PropertyFive,
x.NoOperationRate,
});
方式
protected List<List<object>> DoSomething(
MyModel model,
MyOptions options,
Func<TSomething,string> theValue,
Func<TSomething, bool, List<object>> aMethod)
答案 0 :(得分:-1)
(x, s) => new List<object>
{
Magic(x.PropertyOne, s),
Magic(x.PropertyTwo, s),
Magic(x.PropertyThree, s),
x.PropertyFour,
x.PropertyFive,
x.NoOperationRate,
}
是一个lambda表达式,可以翻译成方法:
public List<object> unnamed(TSomething x, bool s) {
return new List<object>
{
Magic(x.PropertyOne, s),
Magic(x.PropertyTwo, s),
Magic(x.PropertyThree, s),
x.PropertyFour,
x.PropertyFive,
x.NoOperationRate,
};
}
new List<object> { ... }
只是一个快捷方式:
var list = new List<object>();
list.Add(Magic(x.PropertyOne, s));
list.Add(Magic(x.PropertyTwo, s));
...
list.Add(x.NoOperationRate);
内部DoSomething
方法必须是代码:
aMethod(something, someBool);
换句话说,你应该查看DoSomething
函数内部,找出传递给这个长lambda的参数。