【问题标题】:IEnumerable not accepting my class as a type?IEnumerable 不接受我的类作为类型?
【发布时间】:2013-07-02 21:20:29
【问题描述】:

我正在尝试转换一组动态值,以便我可以使用 lambda 表达式来查询它们,但是在将集合转换为 IEnumerable 时我不断收到错误:

(IEnumerable<MyClass>ViewBag.MyClassList)

导致错误

Using the generic type System.Collections.Generic.IEnumerable<T> requires 1 type arguments

MyClass is a 'type', which is not valid in the given context

这实际上是零意义。 IEnumerable 想要一个类型,但使用的是 is not valid in the given context 类型。

停下来?

【问题讨论】:

  • 可能是括号的问题。试试((IEnumerable<MyClass>) ViewBag.MyClassList)
  • 那么您应该显示一些相关代码,以便我们也可以帮助您。

标签: asp.net-mvc collections types ienumerable


【解决方案1】:

括号应该可以解决问题:

((IEnumerable<MyClass>) ViewBag.MyClassList)

【讨论】:

    【解决方案2】:

    没有太多关于上下文的信息。

    ViewBag.MyClassList as IEnumerable<MyClass>

    也可能是一两个扩展方法

    ViewBag.MyClassList.ConvertAll<MyClass>()

    ViewBag.MyClassList.ToList<MyClass>()

    但除此之外,Jeroen 有“CAST”方法和括号用法

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多