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

时间:2013-01-25 15:47:33

标签: c# linq entity-framework

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();
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:19)

在linq-to-entities中,你不能将Contains与类一起使用,你只能使用它与原始类型,所以你需要改变它:

where t.ttCategories.Contains(c)

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

答案 1 :(得分:1)

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