Using DefaultIfEmpty in Linq - Problem Replacing Zero Value for Default Value

I have questions that may or may not have question_group

if all questions have no question_group, and if I use default, if empty, like this:

question_group defaultQuestion = new question_group {question_group_id = Guid.Empty};
questions.Select(x => x.question_group).DefaultIfEmpty(defaultQuestion).Distinct();

Should I not get IEnumerable<question_group>that contains only the default question group that I defined? I get null .... what am I missing here?

+5
source share
1 answer

, DefaultIfEmpty , . , , , null. DefaultIfEmpty ( ). , , .

, , , , , , , . - :

var questionGroups = questions.Select(x => x.question_group)
                              .Where(x => x != null)
                              .DefaultIfEmpty(defaultQuestion)
                              .Distinct();
+6

All Articles