将if if转换为linq

时间:2014-01-07 07:39:33

标签: c# linq

我在c#中有以下代码,我正在寻找最低价格航班。现在我想将其转换为Linq

for (; count < _flightSearchController.ListOfContracts.Count; count++)
            {
                contract = (DTContract)_flightSearchController.ListOfContracts[count];
                if (contract.CurrentStatus == AvailabilityStatus.AVAILABLE)
                {
                    if (CheckContractCitiesWithSearchCriteria(contract, originAirports, destinationAirports))
                    {
                        //if fare is lower than selected contract.
                        if (lowestPriceContract == null || lowestPriceContract.FareDetails.PriceForDefaultFlightSelection > contract.FareDetails.PriceForDefaultFlightSelection)
                        {
                            lowestPriceContract = contract;
                        }
                        else if (lowestPriceContract.FareDetails.PriceForDefaultFlightSelection == contract.FareDetails.PriceForDefaultFlightSelection)
                        {
                            if (lowestPriceContract.FareDetails.PriceAdult > 0 && (lowestPriceContract.FareDetails.PriceAdult + lowestPriceContract.FareDetails.FareTaxAdult) > (contract.FareDetails.PriceAdult + contract.FareDetails.FareTaxAdult))
                            {
                                lowestPriceContract = contract;
                            }
                            else if (lowestPriceContract.FareDetails.PriceSenior > 0 && (lowestPriceContract.FareDetails.PriceSenior + lowestPriceContract.FareDetails.FareTaxSenior) > (contract.FareDetails.PriceSenior + contract.FareDetails.FareTaxSenior))
                            {
                                lowestPriceContract = contract;
                            }
                        }

                    }

}

我尝试过转换,但如果是其他部分则卡在其中。

   var q = _flightSearchController.ListOfContracts.ToList<DTContract>()
                    .Where(cont => cont.CurrentStatus == AvailabilityStatus.AVAILABLE);
                if (lowestPriceContract == null || lowestPriceContract.FareDetails.PriceForDefaultFlightSelection > contract.FareDetails.PriceForDefaultFlightSelection)
                {


                }

1 个答案:

答案 0 :(得分:2)

使用Min扩展名方法:

var q = _flightSearchController.ListOfContracts
            .Where(cont => cont.CurrentStatus == AvailabilityStatus.AVAILABLE 
                 && CheckContractCitiesWithSearchCriteria(cont, originAirports, destinationAirports))
            .Min(cont=> cont.FareDetails.PriceForDefaultFlightSelection)

编辑我已经掩盖了决胜局部分,这让它变得有点复杂。你可以通过排序来做到这一点,但是当有很多合同时,这会更慢:

var q = _flightSearchController.ListOfContracts
            .Where(cont => cont.CurrentStatus == AvailabilityStatus.AVAILABLE)
                && CheckContractCitiesWithSearchCriteria(cont, originAirports, destinationAirports))
            .OrderBy(cont => FareDetails.PriceForDefaultFlightSelection)
            .ThenBy(cont => cont.FareDetails.PriceAdult + lowestPriceContract.FareDetails.FareTaxAdult)
            .ThenBy(cont => cont.FareDetails.PriceSenior + lowestPriceContract.FareDetails.FareTaxSenior)
            .First();

您可以为IComparable对象实现FareDetails界面来比较价格,这样您就可以执行此操作:

var q = _flightSearchController.ListOfContracts
        .Where(cont => cont.CurrentStatus == AvailabilityStatus.AVAILABLE 
             && CheckContractCitiesWithSearchCriteria(cont, originAirports, destinationAirports))
        .Min(cont=> cont.FareDetails)