【问题标题】:Lambda Expression with Inner Lambda Expression带有内部 Lambda 表达式的 Lambda 表达式
【发布时间】:2013-07-02 12:49:03
【问题描述】:
The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)

当我尝试从下面运行代码时,我从上面得到错误。

this.Calendar.Entries.Any<CalendarEntry>(c => c.Date.Date == date.Date && Filters.Any<Type>(f => typeof(c).IsInstanceOfType(f)));

有谁知道为什么这不起作用?如果我可以让它工作?

谢谢。

编辑:

现在仍然知道为什么它不像我最初写的那样工作,但是当我这样写时它工作:

Filters.Any<Type>(f => this.Calendar.Entries.Where<CalendarEntry>(c => c.Date.Date == date.Date).SingleOrDefault().GetType().IsInstanceOfType(f));  

【问题讨论】:

    标签: c# .net lambda closures


    【解决方案1】:

    typeof 适用于类型名称。如果您需要c 的运行时类型,则必须使用Object.GetType 并说c.GetType()

    因此,编译器看到typeof(c) 并知道typeof 只接受类型名称,因此尝试勇敢地 在某处、任何地方找到a type named c,但可惜它不能。所以,它告诉你“我找不到类型 c。”

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多