EF does not recognize the method 'Boolean Contains (System.Object)' method, and this method can not be translated into a store expression

advertisements

I am trying to get objects from database based on a list of ids, but I get the error

LINQ to Entities does not recognize the method 'Boolean Contains(System.Object)' method, and this method cannot be translated into a store expression

Here is my code

       shopingEntities db = new shopingEntities();
      var q = (from a in db.CatSetups
                     join b in db.CatofCats
                     on a.CatId equals b.ParentCatID
                     where a.CatId == 1

                     select new
                     {

               // ID        b.ChildCat

                     });

      ArrayList list = new ArrayList(q.ToList());
      var ok = (from cc in db.CatSetups
                where list.Contains(cc.CatId)
                select cc).ToList();


If I understood well your problem, you are trying to find child categories for a given Category Id

var childCats=from a in db.CatSetups
               join b in db.CatofCats on a.CatId equals b.ParentCatID
               where a.CatId == 1
               select  b.ChildCat;//Assuming ChildCat is an integer

var cats=db.CatSetups.Where(c=>childCats.Contains(c.CatId)).ToList();

In case that CatofCats is the junction table in a many to many relationship and if you were using navigation properties:

public class CatofCats
{
    public int ParentCatID{get;set;}

    public int ChildCatID{get;set;}

    public virtual CatSetups ParentCat{get;set;}
    public virtual CatSetups ChildCat{get;set;}
}

Then your query would be:

var childCats=db.CatofCats.Where(c=>c.ParentCatID==1).Select(c=>ChildCat);