根据子集合汇总属性

时间:2012-09-20 17:45:42

标签: c# asp.net linq linq-to-entities rdlc

我觉得自己就像一个踢腿比赛的单腿男人...我需要在我的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; }
    }
    

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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#不允许属性名称与类名相同。