4

我觉得自己像一个踢屁股比赛的单腿人......我需要向我的 LINQ 数据集添加一个属性,该属性提供基于子集合属性的摘要,然后展平我的数据。(我正在创建一个 .rdlc 数据集。)我终于想出了如何展平我的数据,但我无法弄清楚如何根据哪一组审阅者提供他们的评估来得出一个值。

我有一些评估,我需要根据审稿人所属的审稿人组总结一个结果:

  1. 如果审查组中的任何评估是“大问题”,则返回“大问题”。

  2. 如果所有评估都说“没问题”,则返回“没问题”。

  3. 如果没有评估是“大问题”并且至少有一个是空白的,则返回 null。

    下面是我创建的一个示例集,希望能证明我在说什么:

    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

2 回答 2

1

尝试,

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

将评估属性的名称更改为 AssessmentStr,因为 C# 不允许属性名称与类名称相同。

于 2012-09-20T18:16:46.877 回答
1

您可以定义一个方法,并在投影期间调用它。

注意:将 Assessment.Assessment 属性更改为 Assessment.AssessmentDescription,因此属性名称与封闭类型不同

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;
}
于 2012-09-20T18:08:30.300 回答