无法创建常量值。 只有原始类型

dbEntities db = new dbEntities(); foreach (ttCategory c in db.ttCategories) { var tags=(from t in db.ttproduktes where t.ttCategories.Contains(c) select t.ttTags); foreach (ttTag t in tags) // here it says: // Unable to create a constant value - only primitive types { t.ToString(); } } 

我究竟做错了什么?

在linq-to-entities中,您不能将Contains与类一起使用,您只能将它与原始类型一起使用,因此您需要更改它:

 where t.ttCategories.Contains(c) 

  where t.ttCategories.Any(x => x.UniqueProperty == c.UniqueProperty) 
 var tags = (from t in db.ttproduktes where t.ttCategories.Any(q => q.Id == c.Id) select t.ttTags);