如何根据Linq中的值从众多列表中选择一个?

时间:2017-11-14 19:21:25

标签: c# linq

我有一个看起来像这样的对象结构。

public class DeliveryManagerQuoteResponse 
{
    public string OrderId { get; set; }
    public List<DeliveryManagerQuotes> Quotes { get; set; }
}

public class DeliveryManagerQuotes
{
    public bool IsSuccess { get; set; }
    public List<DeliveryManagerQuoteDetails> QuoteDetails { get; set; }
}

public class DeliveryManagerQuoteDetails
{
    public string QuoteId { get; set; }
    public int? DeliveryFee { get; set; }
}

当我填充DeliveryManagerQuoteResponse时,我会有多个Quotes,每个Quotes会有多个QuoteDetails。我想要的是,基于DeliveryFee DeliveryManagerQuoteDetailsDeliveryManagerQuoteResponse,我想选择最低交付费用的DeliveryFee。我怎么能用LINQ做到这一点?

我在考虑从Quotes首先获取public void PickDeliveryQuoteBasedOnRate(DeliveryManagerQuoteResponse deliveryManagerQuoteResponse) { var feeList = new List<int>(); foreach (var quote in deliveryManagerQuoteResponse.Quotes) { if (quote.QuoteDetails != null && quote.QuoteDetails.Any()) { foreach (var quoteDetail in quote.QuoteDetails) { feeList.Add(quoteDetail.DeliveryFee ?? default(int)); } } } if (feeList.Any()) { // This will give me minimum fee var minimumDeliveryFee = feeList.Min(); // Need LINQ to use this minimum fee to pick one quote. } } ,然后编写一些代码/ LINQ来使用它来获取最低费用的报价,但不知道如何编写它:

<!DOCTYPE html>
    <html>
    <head>
    <style>
    div.container {
        width: 100%;
        border: 1px solid gray;
    }
    
    header, footer {
        padding: 1em;
       	border-top: 1px solid black;
        clear: left;   
    }
    
    nav {
        float: left;
        max-width: 160px;
        margin: 0;
        padding: 1em;
    }
    
    nav ul {
        list-style-type: none;
        padding: 0;
    }
       
    nav ul a {
        text-decoration: none;
    }
    
    article {
        margin-left: 170px;
        border-left: 1px solid gray;
        
        overflow: hidden;
    }
    </style>
    </head>
    <body>
    <div class="container">  
    <nav>
      <p>roundel container</p>
    </nav>
    <article>
      <p>article body</p>
    </article>
    </div>
    </body>
    </html>

屏幕截图以便更好地理解:

enter image description here

2 个答案:

答案 0 :(得分:4)

使用最少DeliveryFee返回QuoteDetail:

//button[@class='btn btn-flat btn-no-text btn-kebab-view']

返回报价最小交货时间为Detil,因此将报价与每个QuoteDetail一起转移到最后一个表达式中使用它返回:

//i[@class='zmdi zmdi-more-vert']

答案 1 :(得分:0)

我建议不要将细节拉到顶部然后对引号的多个副本进行排序,而是建议按所有DeliveryFee s的分钟对引号进行排序:

var minDelivery = deliveryManagerQuoteResponse.OrderBy(
                                                  dmqr => dmqr.Quotes // foreach quote
                                                              .SelectMany(q => q.QuoteDetails // foreach QuoteDetail
                                                                                .Where(qd => qd.DeliveryFee.HasValue) // that has a value
                                                                                .Select(qd => qd.DeliveryFee.Value) // select the DeliveryFree
                                                              ) // and flattten the result
                                                              .Min() // and then select the minimum DeliveryFree
                                              ) // sort quotes by minimum DeliveryFee
                                              .FirstOrDefault(); // and select the first quote