I was trying to create a Linq query which unwinds lists. It works all fine if the list contained in the outer object has at least one element. However, for empty lists I would need to add the condition { “preserveNullAndEmptyArrays”, true }. I tried to use DefaultIfEmpty() but it has no effect as in:
query.SelectMany(outerObject => outerObject.InnerArray.DefaultIfEmpty(), (o, a)...
However, it seems as if there is support in the driver but in only works for left outer joins. I could easily modify the driver to accomplish what I want by changing the code in MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelectMany line 340 as follows:
var groupJoin = node.Source as GroupJoinExpression;
if (groupJoin != null && isLeftOuterJoin)
→
if (isLeftOuterJoin)
However, I most certainly break something else by doing this.
Is there any specific reason why SelectMany does not support DefaultIfEmpty()?
Otherwise I kindly request to have it added.
Thanks!
Björn