c# – 无法创建一个常量值.只有原始类型

前端之家收集整理的这篇文章主要介绍了c# – 无法创建一个常量值.只有原始类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. dbEntities db = new dbEntities();
  2. foreach (ttCategory c in db.ttCategories)
  3. {
  4. var tags=(from t in db.ttproduktes where t.ttCategories.Contains(c) select t.ttTags);
  5. foreach (ttTag t in tags) // here it says:
  6. // Unable to create a constant value - only primitive types
  7. {
  8. t.ToString();
  9. }
  10. }

我究竟做错了什么?

解决方法

在linq到实体中,你不能使用包含一个类,你只能使用它与一个基本类型,所以你需要改变这一点:
  1. where t.ttCategories.Contains(c)

  1. where t.ttCategories.Any(x => x.UniqueProperty == c.UniqueProperty)

猜你在找的C#相关文章