Ok, this has been a pretty interesting problem for me to solve, so I will try and do my best to ask this in a way in which you can understand.
So, I have a collection of objects which contains a collection of objects which contains a collection of objects which has a property that sometimes can be null.
It looks something like this pseudocode:
Class ObjectA
{
public IEnumerable<ObjectB>
}
Class ObjectB
{
public IEnumerable<ObjectC>
}
Class ObjectC
{
property? a;
}
Ok, now I basically need to filter out all of the ObjectC where the property = someValue or the property is null.
I tried this: (remember, this is just an example, not real code)
IEnumerable<ClassA> collection;
List<string> filters; // This contains a list of filters
collection = collection.Where(a => a.collectionB.All(b =>
b.collectionC.Where(c => !filter
.Contains(c.Property))?
.Count() == 0))
.ToList();
So, the problem is that if I match a filter to the c.Property
nothing happens. It should remove that one from the collection
but it isn't. I also need to not filter out any of the ones where the c.Property == null
or the collectionC == null
.
EDIT: what I am really trying to accomplish is if some c.Property = 'x' and some c.Property = 'y' and some c.Property = null, I want to remove from my collection
anything where c.Property = 'x' but leave the rest.
collection.Where(a=>a.collectionB.Any(b=> b.collectionC == null ||
b.collectionC.Any(c=>c.Property == null || filters.Contains(c.Property))))