0

I have a table with Sub-Categories of Bike (Mountain Bikes, Touring Bikes, Road Bikes, ...) and I created a column isSelected with Boolean data type. I want to set up in Database when isSelected = true each Sub-Categories is matched with it will be show on Homepage and vice versa.

IQueryable<ProductSubcategory> list = null;

if (Id == null)
{
    list = BikesDB.ProductSubcategories;
}
else
{
    int id = Id.Value;
    list = BikesDB.ProductSubcategories.Where(m => m.ProductSubcategoryID == id
                                                   && m.NameofBike == Name
                                                   && m.isSelected == true);
}

var bikes = list.AsEnumerable().Select(
             p => new Bike { Id = p.ProductSubcategoryID, Name = p.NameofBike });

var viewModel = new CategoriesIndexViewModel
                {
                    NumberOfModel = bikes.Count(),
                    NameofBike = bikes.Select(b=>b.Name).ToList(),
                    Bikes = bikes.ToList()
                };

return this.View(viewModel);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Trung Pham
  • 233
  • 2
  • 6
  • 19
  • 3
    What is the problem you're having? – Jeroen Vannevel Mar 19 '14 at 02:57
  • For example, when I clicked `false` of `Mountain Bikes`, it still display on Homepage of Web Application. How can I disappear `Mountain Bikes` when I set it `false` in Database? – Trung Pham Mar 19 '14 at 03:01
  • 1
    you mean like: `m.isSelected == true` – johnb003 Mar 19 '14 at 03:02
  • @johnb003 Exactly, that's what I mean! – Trung Pham Mar 19 '14 at 03:04
  • You want to create [a dynamic LINQ expression](http://stackoverflow.com/questions/21077888/building-linq-generic-queries). – Jeroen Vannevel Mar 19 '14 at 03:11
  • This question seems specific to your data, and has a lot of irrelevant information. Maybe you should revise your question so that you're showing just the class, an enumerator with a few items, and how you're filtering the list, print the results and show what's not working about it. – johnb003 Mar 19 '14 at 03:21

1 Answers1

1

Are you sure isSelected is actually set? I think your linq expression looks fine. It should already correctly only select items where isSelected == true.

I'd look at your list in the debugger right before the query is done and make sure it has what you think it has.

OR you're running the first branch: if (Id == null) and your list is not filtered by the isSelected condition.

johnb003
  • 1,821
  • 16
  • 30