Could it be that you get the wrong result because of your one &
instead of &&
?
So you have a class Tag_Group
. Every object of Tag_Group
has zero or more Tags
.
Every Tag
has a string CategoryName
and an IEnumerable Tag_List
that contains zero or more Items
. Every Item
has an int(?) property ItemOnStock
. The int
part is not important. It can be any class as long as you can compare it with zero.
It seems you want all Tag_Group
objects that have at least one Tag
in its sequence of Tags
that:
- Has a
CategoryName
equal to "|" + CategoryID + "|"
- AND has at least one item in
Tag_List
that has an ItemOnStock > 0
Or could it be that you want all Tag_Group
objects that
- Has at least one
Tag
in Tags
with a CategoryName
equal to "|" + CategoryID + "|"
- OR has at least one
Tag
in Tags
that has at leas one item in Tag_List
that has an ItemOnStock > 0
. This Tag
may be a different Tag
than the one with the CategoryName
I assume you want the AND option.
Although you didn't say so, I assume db is your dbContext, and you want your query to be performed by the database, so AsQueryable, and not AsEnumerable.
var CategoryId = ...
string categoryName = "|" + CategoryId + "|";
var result = db.Tag_Group
// I want only those tagGroupElements that have at least one Tag
// in sequence Tags that matches both conditions:
.Where(tagGroupElement => tagGroupEleent.Tags
// the tagGroupElement should have a matching CategoryName
.Where(tag => tag.CategoryName == categoryName)
// and the tagGroupElement should have at least one item
// in Tag_List with ItemOnStock > 0
&& tag.Tag_List
.Where(tagListElement => tagListElement.ItemOnStock > 0)
.Any())
.Any());