有人可以帮助解释以下代码中发生的事情吗?非常感谢!结果是 meo,但我不明白这两个“where”在这种情况下是如何工作的。
public class Cat {
public string Text { get; set; }
public Cat Where(Func<Cat,bool> cond) {
return new Cat {
Text = cond(this)? this.Text.ToUpper(): this.Text.ToLower()
}; }
}
public static class CatExtensions {
public static T Select<T>(this Cat cat, Func<Cat,T> proj)
{
return proj(cat);
}
}
var moggy = new Cat { Text = "Meo" };
var result = from m in moggy
where true
where false
select m.Text;
最佳答案
如果您查看该表达式的方法链语法版本,会更容易理解:
moggy
.Where(m => true) // returns new Cat { Text = "MEO" }
.Where(m => false) // returns new Cat { Text = "meo" }
.Select(m => m.Text); // returns "meo"
关于C# 在哪里选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557326/