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

dan*_*iel 10 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Adu*_*cci 19

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

where t.ttCategories.Contains(c)
Run Code Online (Sandbox Code Playgroud)

 where t.ttCategories.Any(x => x.UniqueProperty == c.UniqueProperty)
Run Code Online (Sandbox Code Playgroud)