Does not contain definition for 'Contains'

12,515

This is where var becomes the root of all evil as it hides the real data type. Drop the var and you'll notice the problem immediately:

int[] brands = ids.ToArray();
IQueryable<Brand> brandId = _db.Brands.Where(p => brands.Contains(p.ID));
IQueryable<Product> srtItems = _db.Products.Where(p => p.CategoryID == brandId.ID);

brandId is a sequence (when materialized) and it is not a single object, so it doesn't contains a definition of ID.

To solve that, you can:

IQueryable<int> brandIds = _db.Brands.Where(p=> brands.Contains(p.ID)).Select(b=> b.ID);
IQueryable<Product> srtItems = _db.Products.Where(p=> brandIds.Contains( p.CategoryID));

Get the IDs of the retrieved records, and match by those IDs.

As an advice, don't use var unless the real type is redundant (meaning it shows in other parts of the statement)

Share:
12,515
user777
Author by

user777

Updated on June 14, 2022

Comments

  • user777
    user777 almost 2 years

    It is showing the error like "doesn't contain definition for ID" for the code

    [HttpPost]
    public ActionResult show(List<int> ids)
        {
            if (ids != null)
            {
                int[] brands = ids.ToArray();
                var brandId = _db.Brands.Where(p => brands.Contains(p.ID));
                var srtItems = _db.Products.Where(p => p.CategoryID == brandId.ID);
    
                return PartialView("_pView", srtItems);
            }
        }
    

    and for the following code the error is' doesn't contain definition for Contains'

    [HttpPost]
            public ActionResult show(List<int> ids)
            {
                if (ids != null)
                {
                    int[] brands = ids.ToArray();
                    var brandId = _db.Brands.Where(p => brands.Contains(p.ID));
    
                    var sortItemss = _db.Products.Where(p => brandId.Contains(p.CategoryID));
                    return PartialView("_pView", srtItems);
                }
    

    Please guide me

  • Ivan Stoev
    Ivan Stoev over 7 years
    Bad advice. Both your var "replacements" changed the real type from IQueryable<T> to IEnumerable<T>.
  • Zein Makki
    Zein Makki over 7 years
    @IvanStoev Absolutely correct about the second part, but not about the advice! I did that in a hurry, but if someone does this intentionally then that does mean he doesn't know what the method returns.. We should not be using a method that we don't know its signature I guess. Otherwise, we need to fully understand the difference between IQueryable and IEnumerable and What gets executed Where and When
  • user777
    user777 over 7 years
    @user3185569, sir can you please explain what difference does it make between IEnumerable and IQueryable. I mean when to use each of them.
  • Zein Makki
    Zein Makki over 7 years
    @pavan That question already has good answers.. One of which: stackoverflow.com/questions/2876616/…
  • user777
    user777 over 7 years
    @user3185569, Sir, can guide please me for this question, stackoverflow.com/questions/40944709/…