I’m trying to use SelectMany to evaluate a first query with conditions, based on those results execute another query. I’ve been told that SelectMany should be able to do this. The problem I’m having is the SelectMany seems to ignore the conditions in the first lambda expression. My problem may be with the first expression but I’m not really sure. Any ideas what I’m doing wrong.
First
//get source based on user and active flag
var query = _sourceRepository.GetTable().Where(s => s.ActionItemStates.Any(ais => ais.UserId == user.Id && ais.IsActive == active));
Second – ignores conditions in first
var queryActionItems = query.SelectMany(x => x.ActionItemStates).OrderBy(x => x.ActionItem.SortOrder);

Well, your first query doesn’t assert that all
ActionItemStateswill have your userId and IsActive true.It just mean : select me all the
Sourceswhere AT LEAST ONEActionItemStatefulfills my conditions.But in the second query,
ActionItemStatesare no longer submitted to your conditions : your just taking allActionItemStatesfrom theSourcesselected in the first query.First query makes a selection between
Sources, not betweenActionItemStates.Now, this is just explaining WHY it doesn’t work, a real answer would need you to explain clearly what you wanna retrieve !