3

I have a lite problem i don't really know how to fix. In my example below i would like to select a list of ProductCategories with ProductItems that are active.

public IEnumerable<ProductCategory> ListProductCategories()
        {
            return _entities.ProductCategorySet.Include("ProductItems").Where(x => x.ProductItems.Active == true).ToList();               
        }

The problem is that i can't access productItem property Active in my lambda expression, what is the problem? Do I think totaly wrong when im trying to write a linq query like the one above?

1 Answer 1

6

There could be more than one item. You probably want to select the categories where all items are active:

return _entities.ProductCategorySet
                .Include("ProductItems")
                .Where(x => x.ProductItems.All(item => item.Active))
                .ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

the solution didn't really solve the problem. Your solution only return productcategories with any productitems. It dosn't matter if they are active or not. What i want to return is all productcategories and productitems only where productitems is active == true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.