rhdf
rhdf

Reputation: 11

List only categories that have products

I have a database with following structure:

Categories
[PK]CatID
CatName

ProductTypes
[PK]TypeID
TypeName

Manufacturer
ManufacturerID
Name

Products
[PK]ProdID
Title
TypeID
ManufacturerID
IsDeleted (bool)
IsHidden (bool)

ProductTypes are "irrelevant" for my application, but are necessary for export to another system.

I want to get a nested list with categories and the products filtered on Products.IsDeleted and Products.IsHidden Like this

Category 1
Product.ProdID - Product.Title, Manufacturer.Name ...

Category 2
Product.ProdID - Product.Title, Manufacturer.Name ...

How to do this in the simplest way with EF

var catList = db.TypeIDs.Where(t => t.product.Count() > 0).Select(x => x.TypeCat).Distinct().ToList();   

This gives me the categories, but no filtering for deleted/hidden products

Upvotes: 1

Views: 72

Answers (1)

LInsoDeTeh
LInsoDeTeh

Reputation: 1038

Instead of

.Where(t => t.product.Count() > 0)

Use

.Where(t => t.product.Any(u => !u.IsHidden && !u.IsDeleted))

Upvotes: 2

Related Questions