【问题标题】:Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context错误:无法创建“System.Object”类型的常量值。此上下文仅支持原始类型或枚举类型
【发布时间】:2018-03-05 16:52:02
【问题描述】:

我有以下代码:

public ActionResult OnDemand()
{
    List<SiteMenu> all = new List<SiteMenu>();
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        all = dc.SiteMenus.Where(a => a.ParentMenuID.Equals(0)).ToList();
    }

    return View(all);
}

...但我收到错误消息:无法创建类型为“System.Object”的常量值。此上下文仅支持原始类型或枚举类型

...错误发生在以下行:

all = dc.SiteMenus.Where(a => a.ParentMenuID.Equals(0)).ToList();

我可以就我做错了什么寻求帮助吗? ...提前致谢

【问题讨论】:

  • 显然ParentMenuID 不是整数。尝试使用相等运算符 == 而不是 Equals 并查看是否出现任何编译时错误。

标签: c# asp.net linq


【解决方案1】:

这应该可以正常工作:

all = dc.SiteMenus.Where(a => a.ParentMenuID == 0).ToList();

如例外所述:在此上下文中仅支持 原始类型枚举类型。这意味着ParentMenuIDobject type

为了使用.Equals(),它应该是primitive typeenumeration type

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多