2

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

user777
  • 77
  • 1
  • 1
  • 10

1 Answers1

7

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)

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Bad advice. Both your `var` "replacements" changed the real type from `IQueryable` to `IEnumerable`. – Ivan Stoev Nov 30 '16 at 08:17
  • @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* – Zein Makki Nov 30 '16 at 08:18
  • @user3185569, sir can you please explain what difference does it make between IEnumerable and IQueryable. I mean when to use each of them. – user777 Nov 30 '16 at 18:53
  • @pavan That question already has good answers.. One of which: http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet?noredirect=1&lq=1 – Zein Makki Nov 30 '16 at 19:30
  • @user3185569, Sir, can guide please me for this question, http://stackoverflow.com/questions/40944709/using-distinct-in-lambda-expression-and-use-it-in-foreach-loop/40944866#40944866 – user777 Dec 03 '16 at 06:25