score:1

Accepted answer

You can define a method, and call it during the projection.

Note: Changed Assessment.Assessment property to Assessment.AssessmentDescription so the property name would not be the same as the enclosing type

public List<SummaryReport> CreateReport()
{
 return Get()
     .SelectMany(rev => rev.Assessments, (rev, ass) => new { rev, ass })
     .Select(x => new SummaryReport
     {
         ReviewID = x.rev.ReviewID,
         ReviewerID = x.ass.Reviewer.ReviewerID,
         Assessment = x.ass.Assessment,
         ReviewGroupID = x.ass.Reviewer.ReviewGroup.ReviewGroupID,
         GroupAssessment = DeriveGroupAssessment(x.rev.Assessments)
      })
     .ToList();
 }


private static string DeriveGroupAssessment(IEnumerable<Assessment> assessments)
{
  string assessment = null;

  if (assessments.Any(a => a.AssessmentDescription == "Big Problem"))
  {
    assessment = "Big Problem";
  }
  else if (assessments.All(a => a.AssessmentDescription == "No Problem"))
  {
    assessment = "No Problem";
  }

  // If you want the value to be null if neither of the above conditions are met, this logic is not needed
  ////if (!assessments.Any(a => a.AssessmentDescription == "Big Problem")
  ////    && assessments.Any(a => a.AssessmentDescription == string.Empty))
  ////{
  ////  assessment = null;
  ////}

  return assessment;
}

score:1

Try,

 public static List<SummaryReport> CreateReport()
    {
        return Get()
            .SelectMany(rev => rev.Assessments, (rev, ass) => new { rev, ass })
            .Select(x => new SummaryReport
            {
                ReviewID = x.rev.ReviewID,
                ReviewerID = x.ass.Reviewer.ReviewerID,
                Assessment = x.ass.AssessmentStr,
                ReviewGroupID = x.ass.Reviewer.ReviewGroup.ReviewGroupID,
                GroupAssessment = x.rev.Assessments.Any(a => a.AssessmentStr == "Big Problem") ? "Big Problem" : 
                x.rev.Assessments.All(a => a.AssessmentStr == "No Problem")? "No Problem" : null
            })
            .ToList();
    }

Changed name of Assessment property to AssessmentStr because C# not allow property name same as class name.


Related Articles