Summarize a property based on a subcategory

I feel like a one-legged man in a bum contest ... I need to add a property to my LINQ dataset that contains a summary based on the properties of the subnets and then smooths my data. (I am creating a .rdlc dataset.) I finally figured out how to smooth my data, but I can’t figure out how to get a value based on which reviewer group provided their assessment.

I have some ratings, and I need to summarize one result based on the reviewer group to which the Reviewer belongs:

  • If any of the ratings in the review group is β€œBig Problem,” return β€œBig Problem.”
  • If all ratings say "No problem," return "No problem."
  • If no ratings are a "big problem" and at least one of them is empty, return zero.

    Below is a sample I created to hopefully demonstrate what I'm talking about:

    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 = "Need to derive this value!!??!?" }) .ToList(); } public List<Review> Get() { ReviewGroup GrpOne = new ReviewGroup(){ ReviewGroupID=1}; ReviewGroup GrpTwo = new ReviewGroup(){ ReviewGroupID=2}; ReviewGroup GrpThree = new ReviewGroup(){ ReviewGroupID=3}; ReviewerInfo userOne = new ReviewerInfo() { ReviewerID=1, ReviewGroup = GrpOne}; ReviewerInfo userTwo = new ReviewerInfo() { ReviewerID=2, ReviewGroup = GrpTwo}; ReviewerInfo userThree = new ReviewerInfo() { ReviewerID=3, ReviewGroup = GrpTwo}; ReviewerInfo userFour = new ReviewerInfo() { ReviewerID=4, ReviewGroup = GrpThree}; Assessment asOne = new Assessment() { AssessmentID=1, Reviewer=userOne, Assessment="Big Problem"}; Assessment asTwo = new Assessment() { AssessmentID=2, Reviewer=userTwo, Assessment="No Problem"}; Assessment asThree = new Assessment() { AssessmentID=3, Reviewer=userThree, Assessment="No Problem"}; Assessment asFour = new Assessment() { AssessmentID=4, Reviewer=userFour, Assessment=""}; Assessment asFive = new Assessment() { AssessmentID=5, Reviewer=userOne, Assessment="No Problem"}; Assessment asSix = new Assessment() { AssessmentID=6, Reviewer=userTwo, Assessment="No Problem"}; Assessment asSeven = new Assessment() { AssessmentID=7, Reviewer=userThree, Assessment="No Problem"}; Assessment asEight = new Assessment() { AssessmentID=8, Reviewer=userFour, Assessment="No Problem"}; Assessment asNine = new Assessment() { AssessmentID=9, Reviewer=userOne, Assessment="No Problem"}; Assessment asTen = new Assessment() { AssessmentID=10, Reviewer=userTwo, Assessment=""}; Assessment asEleven = new Assessment() { AssessmentID=11, Reviewer=userThree, Assessment="No Problem"}; Assessment asTwelve = new Assessment() { AssessmentID=12, Reviewer=userFour, Assessment="No Problem"}; List<Assessment> firstList = new List<Assessment>() { asOne, asTwo, asThree, asFour }; List<Assessment> secondList = new List<Assessment>() {asFive, asSix, asSeven, asEight}; List<Assessment> thirdList = new List<Assessment>() { asNine, asTen, asEleven, asTwelve }; Review revOne = new Review() { ReviewID=1, Assessments=firstList }; Review revTwo = new Review() { ReviewID=2, Assessments = secondList }; Review revThree = new Review() { ReviewID = 3, Assessments = thirdList }; List<Review> reviews = new List<Review>() { revOne, revTwo, revThree }; return reviews; } public class SummaryReport { public int ReviewID { get; set; } public int ReviewerID { get; set; } public string Assessment { get; set; } public int ReviewGroupID { get; set; } public string GroupAssessment { get; set; } } public class Review { public int ReviewID { get; set; } public virtual List<Assessment> Assessments { get; set; } public Review() { } } public class Assessment { public int AssessmentID { get; set; } public ReviewerInfo Reviewer { get; set; } public string Assessment { get; set; } } public class ReviewerInfo { public int ReviewerID { get; set; } public ReviewGroup ReviewGroup { get; set; } } public class ReviewGroup { public int ReviewGroupID { get; set; } } 
+4
source share
2 answers

You can define a method and call it during projection.

Note. The Assessment.Assessment property has been changed to evaluate .AssessmentDescription so that the name of the property will 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; } 
+1
source

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(); } 

The Assessment property name has been changed for EvaluationStr because C # does not resolve the property name in the same way as the class name.

+1
source

Source: https://habr.com/ru/post/1435345/


All Articles