如何在lambda表达式中使用运算符(if else) [英] How to use operators(if else) in lambda expression
问题描述
CurrentFinancialMovementData movementData = keys.OrderByDescending(s => s.LatestReserveDate ?? s.LatestRecoveryReserveDate).ThenByDescending(x => x.ReserveDaySequence).First();
CurrentFinancialMovementData movementDataReserve = keys.Where(s => s.AmountType ==(short)StaticValues.AmountType.Reserve).OrderByDescending(a => a.LatestReserveDate).ThenByDescending(x => ; x.ReserveDaySequence).FirstOrDefault();
CurrentFinancialMovementData movementDataRecoveryReserve = keys.Where(s => s.AmountType ==(short)StaticValues.AmountType.RecoveryReserve).OrderByDescending(a => a.LatestRecoveryReserveDate).ThenByDescending(x => ; x.ReserveDaySequence).FirstOrDefault();
我想检查我的movingData AmountType.Reserve然后它必须采用LatestReserveDate并且如果我的movementData是AmountType.RecoveryReserve,它应该采用LatestRecoveryReserveDate。我想在movingData中处理这种情况。任何人都可以帮助我吗?
我尝试过:
我试过:
if(keys.Any(s => s.AmountType ==(short)StaticValues.AmountType.Reserve))
{CurrentFinancialMovementData movementData = keys .OrderByDescending(s => s.LatestReserveDate).ThenByDescending(x => x.ReserveDaySequence).First();
}
else if(keys.Any(s => s.AmountType ==(short)StaticValues.AmountType.RecoveryReserve))
{CurrentFinancialMovementData movementData = keys.OrderByDescending(s => s.LatestRecoveryReserveDate).ThenByDescending(x => x.ReserveDaySequence).First();
}
我会这样做:
CurrentFinancialMovementData movementData = null ;
{
CurrentFinancialMovementData movement1 = keys.OrderByDescending(s = > s.LatestReserveDate)
.ThenByDescending(x = > x.ReserveDaySequence)
.FirstOrDefault(x => x.AmountType == StaticValues.AmountType.Reserve);
CurrentFinancialMovementData movement2 = keys.OrderByDescending(s = > s.LatestRecoveryReserveDate)
.ThenByDescending(x = > x.ReserveDaySequence)
.FirstOrDefault(x => x.AmountType == StaticValues.AmountType.RecoveryReserve);
movementData =(移动== null )?运动2:运动;
}
// 警告 - 仍然有可能moveData可能为null ,但
// 在确定这一事实的过程中优雅地处理,以及你是什么
// 这些信息取决于你。
CurrentFinancialMovementData movementData = keys.OrderByDescending(s => s.LatestReserveDate ?? s.LatestRecoveryReserveDate).ThenByDescending(x => x.ReserveDaySequence).First(); CurrentFinancialMovementData movementDataReserve = keys.Where(s => s.AmountType == (short)StaticValues.AmountType.Reserve).OrderByDescending(a => a.LatestReserveDate).ThenByDescending(x => x.ReserveDaySequence).FirstOrDefault(); CurrentFinancialMovementData movementDataRecoveryReserve = keys.Where(s => s.AmountType == (short)StaticValues.AmountType.RecoveryReserve).OrderByDescending(a => a.LatestRecoveryReserveDate).ThenByDescending(x => x.ReserveDaySequence).FirstOrDefault();
I want to check if my movementData AmountType.Reserve then it must take LatestReserveDate and if my movementData is AmountType.RecoveryReserve than it should take LatestRecoveryReserveDate. I want to handle this situation in movementData. Can anyone help me in this ?
What I have tried:
I tried:
if (keys.Any(s => s.AmountType == (short)StaticValues.AmountType.Reserve))
{ CurrentFinancialMovementData movementData = keys.OrderByDescending(s => s.LatestReserveDate).ThenByDescending(x => x.ReserveDaySequence).First();
}
else if (keys.Any(s => s.AmountType == (short)StaticValues.AmountType.RecoveryReserve))
{ CurrentFinancialMovementData movementData = keys.OrderByDescending(s => s.LatestRecoveryReserveDate).ThenByDescending(x => x.ReserveDaySequence).First();
}
I would do it this way:
CurrentFinancialMovementData movementData = null; { CurrentFinancialMovementData movement1 = keys.OrderByDescending(s => s.LatestReserveDate) .ThenByDescending(x => x.ReserveDaySequence) .FirstOrDefault(x=>x.AmountType == StaticValues.AmountType.Reserve); CurrentFinancialMovementData movement2 = keys.OrderByDescending(s => s.LatestRecoveryReserveDate) .ThenByDescending(x => x.ReserveDaySequence) .FirstOrDefault(x=>x.AmountType == StaticValues.AmountType.RecoveryReserve); movementData = (movement == null) ? movement2 : movement; } // caveat - there's still a chance that movementData could be null, but it's // handled gracefully in the process of determining that fact, and what you // do with that info is up to you.
这篇关于如何在lambda表达式中使用运算符(if else)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!