【问题标题】:Unable to create a constant value of type 'EShop.ClassLibrary.ProductType'. Only primitive types or enumeration types are supported in this context无法创建类型为“EShop.ClassLibrary.ProductType”的常量值。此上下文仅支持原始类型或枚举类型
【发布时间】:2014-12-14 19:20:04
【问题描述】:

我有这 2 个类,产品与 ProductType 相关,但是当使用下面的代码时,我看到以下错误。

无法创建“EShop.ClassLibrary.ProductType”类型的常量值。此上下文仅支持原始类型或枚举类型

它们与 ProductType LinkProductTypeId 相关

 public class Product
{
    [Key]
    public virtual int ProductID { get; set; }
    public virtual ProductType LinkProductTypeId { get; set; }
    public virtual string Name { get; set; }
    public virtual string EngName { get; set; }
    public virtual RelatedProduct ProductRelated { get; set; }
    public virtual decimal BuyCost { get; set; }
    public virtual decimal SellCost { get; set; }
}
 public class ProductType
{
    public virtual int ProductTypeId { get; set; }//id in ef
    public virtual string Name { get; set; }
}

运行时代码是这样的:

 public static List<SelectListItem> GetProductSelectList()
    {
        ProductType type = _db.productType.Single(t => t.ProductTypeId == 1);
        List<Product> m = (_db.product.Where(r => r.LinkProductTypeId == type) ).ToList();//error line

        List<SelectListItem> sa = new List<SelectListItem>();
        foreach (Product item in m)
        {
            sa.Add(new SelectListItem { Text = item.Name, Value = item.ProductID.ToString() });
        }

        return sa;
    }

【问题讨论】:

  • 一句警告。寻求帮助时,您需要传递所有信息。例如,“但它失败了”并没有告诉“它是如何失败的”,也没有告诉它失败的地方(哪一行引发了异常)。阅读:stackoverflow.com/help/how-to-ask。

标签: c# asp.net asp.net-mvc


【解决方案1】:

我认为你要处理的问题是下面这行代码:

List<Product> m = (_db.product.Where(r => r.LinkProductTypeId == type)).ToList();

您似乎在那里尝试获得所有LinkProductTypeId 等于type 的产品,您之前已经获得过。可能您无法使用相等运算符解决这些对象的相等性。

我建议使用ProductTypeId 这样做。代码方面:

List<Product> m = (_db.product.Where(r => r.LinkProductTypeId.ProductTypeId == 
                                          type.ProductTypeId)).ToList();

如果ProductTypeId 对于每个ProductType 来说都是唯一的,看起来就是这样,那么上面的内容就可以完成你的工作。

为什么上面的有效,而前面的无效?

上述方法有效,因为您只是比较比较两个整数,如果它们具有相同的值,则 Where 中的谓词返回 true 并且当前项目将包含在列表中。

前面的不行,因为==在两个引用类型之间是在寻找引用相等而不是为了对象的对应字段有相同的值。

让我们有一个类

public class Customer 
{
    public string FirstName { get; set; }      
    public string LastName { get; set; }
    public int Age { get; set; }

    public Customer(string firstName, string lastName, int age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }
}

然后是这个控制台应用

public class Program
{

    public static void Main()
    {
        var customerA = new Customer("firstname","lastname",22);
        var customerB = new Customer("firstname","lastname",22);

        Console.WriteLine(customerA==customerB);
    }
}

将输出false,因为尽管customerA 和customerB 的所有值都具有相同的值,但customerA 和customerB 是两个不同的对象。请看here。

【讨论】:

  • @user3480060 你欢迎老兄!我很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 2020-10-08
  • 2013-04-30
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多