Unable to create a constant value. Only primitive types

c# entity-framework linq

Question

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

What am i doing wrong?

1
10
1/25/2013 3:47:32 PM

Accepted Answer

In linq-to-entities, you can't use Contains with a class, you can only use it with a primitive type, so you need to change this:

where t.ttCategories.Contains(c)

to

 where t.ttCategories.Any(x => x.UniqueProperty == c.UniqueProperty)
19
1/25/2013 3:52:24 PM

Popular Answer

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow