C# 在哪里选择

标签 c# linq select where-clause func

有人可以帮助解释以下代码中发生的事情吗?非常感谢!结果是 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/

相关文章:

c# - 从标签 `&lt table &lt` 解析的 HTML Agility 包中的选项

c# - LINQ 转换问题

mysql - 计算mysql中一行中的项目数

JavaScript - 根据所选选项隐藏按钮

c# - 在点网中不可能适用于 int 和 uint 的通用方法吗?

c# - 如何以线程安全的方式更新控件

c# - 使用 linq to entities c# 的 gridview_sorting 事件

c# - 重构LINQ to Entities查询

LINQ 从动态表名字符串中选择

c# - 必须至少映射一个属性 Entity Framework 6 错误