c# – 根据子集合汇总属性

发布时间:2020-08-01 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了c# – 根据子集合汇总属性脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我觉得自己就像一个踢腿比赛的单腿男人……我需要在LINQ数据集中添加一个属性,该属性提供基于子集合属性的摘要,然后展平我的数据. (我正在创建一个.rdlc数据集.)我终于想出了如何压平我的数据,但我无法弄清楚如何根据哪一组审阅者提供他们的评估来推导出一个值.

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

>如果审核组中的任何评估为“大问题”,请返回“大问题”.
>如果所有评估都说“没问题”,则返回“无问题”.
>如果没有评估是“大问题”且至少有一个是空白,则返回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,Assessment="No Problem"};
    Assessment asSix = new Assessment() { AssessmentID=6,Assessment="No Problem"};
    Assessment asSeven = new Assessment() { AssessmentID=7,Assessment="No Problem"};
    Assessment asEight = new Assessment() { AssessmentID=8,Assessment="No Problem"};

    Assessment asNine = new Assessment() { AssessmentID=9,Assessment="No Problem"};
    Assessment asTen = new Assessment() { AssessmentID=10,Assessment=""};
    Assessment asEleven = new Assessment() { AssessmentID=11,Assessment="No Problem"};
    Assessment asTwelve = new Assessment() { AssessmentID=12,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; }
}

解决方法

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

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

public List<SummaryReport> CreateReport()
{
 return Get()
     .SelectMany(rev => rev.Assessments,ass })
     .Select(x => new SummaryReport
     {
         ReviewID = x.rev.ReviewID,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;
}

总结

以上是脚本之家为你收集整理的c# – 根据子集合汇总属性全部内容,希望文章能够帮你解决c# – 根据子集合汇总属性所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入脚本之家官方QQ群:1065694478
脚本之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!