I have this linq query which retrieves all element in a database, and filters the output based on some criteria..
I want a list of certain entities, with only attributes that conforms to a specific filter
var entitiesWithLookupsTolookupEntityName = schemaRepository
.Many()
.OrderByDescending(x => x.Version)
.Take(1)
.Include(x => x.Entities)
.ThenInclude(x => x.Attributes)
.ThenInclude(x => x.AttributeTypeSpecification)
.SelectMany(x => x.Entities
.Where(x => x.Attributes.Any(y => y.Type == DataType.Lookup && y.AttributeTypeSpecification.EntityInternalName == lookupEntityName))).AsEnumerable();
This returns the only entity which conforms to the filter, but also includes all the attributes of the entity which does not conform to the filter.
I can though in a second query like this filter them away as such
var attributefiltered = entitiesWithLookupsTolookupEntityName.SelectMany(x =>
x.Attributes.Where(y =>
y.Type == DataType.Lookup && y.AttributeTypeSpecification.EntityInternalName == lookupEntityName));
but why can't I combine both of these?
Seems weird that I am able to do it two steps but not one? Something wrong with using .where() .any()