dwa4821
2017-03-01 08:30
采纳率: 71.4%
浏览 1.8k

把if else 转化为拉姆达表达式

如何简化这段代码,用拉姆达表达式

 decimal? money = 0;
        if (grade[0].Grade_level > _Student.Stu_consume)
        {
            money = grade[0].Grade_level - _Student.Stu_consume;
        }
        else if (grade[0].Grade_level < _Student.Stu_consume && grade[1].Grade_level > _Student.Stu_consume)
        {
            money = grade[1].Grade_level - _Student.Stu_consume;
        }
        else if (grade[1].Grade_level < _Student.Stu_consume && grade[2].Grade_level > _Student.Stu_consume)
        {
            money = grade[2].Grade_level - _Student.Stu_consume;
        }
        else if (grade[2].Grade_level < _Student.Stu_consume && grade[3].Grade_level > _Student.Stu_consume)
        {
            money = grade[3].Grade_level - _Student.Stu_consume;
        }
        else if (grade[3].Grade_level < _Student.Stu_consume && grade[4].Grade_level > _Student.Stu_consume)
        {
            money = grade[4].Grade_level - _Student.Stu_consume;
        }
        else if (grade[4].Grade_level < _Student.Stu_consume)
        {
            money = -1;
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2017-03-01 15:30
    已采纳

    假设grade是排序好的,你可以这么写

     money = grade.Select(x => x.Grade_level).FirstOrDefault(x => x > _Student.Stu_consume, _Student.Stu_consume - 1) - _Student.Stu_consume;
    
    点赞 评论

相关推荐 更多相似问题