2017-03-16 76 views
0

我有以下實體結構。我有幾個裝飾,將填充Invoice.InvoiceLines,並在結束時,我想OrderReference使用linq按深度級/實體字段按集合排序

public class Invoice 
    { 
     private readonly IList<InvoiceLine> _invoiceLines; 
     public virtual IList<InvoiceLine> InvoiceLines => _invoiceLines; 

     public Invoice() 
     { 
      _invoiceLines = new List<InvoiceLine>(); 
     } 

    } 

    public class InvoiceLine 
    { 

     private readonly IList<TransactionLine> _transactionLines = new List<TransactionLine>(); 
     public virtual IReadOnlyCollection<TransactionLine> TransactionLines => new ReadOnlyCollection<TransactionLine>(_transactionLines); 

     public virtual void AddTransactionLine(TransactionLine transactionLine) 
     { 
      transactionLine.ThrowIfNull(nameof(transactionLine), "can't be null"); 
      _transactionLines.Add(transactionLine); 
     } 
    } 


    public class PermanentPlacementTransactionLine : TransactionLine 
    { 

     public PermanentPlacement PermanentPlacement { get; set; } 
    } 


     public class PermanentPlacement 
    { 
     public virtual string JobReference { get; set; } 
     public virtual string OrderReference { get; set; } 
    } 

我想要做的是OrderReference訂單發票行命令實施invoicelines。

foreach (var invoice in DecoratedResult.Invoices.Where(inv => inv.InvoiceType == InvoiceType.Weekly)) 
      { 
       var customer = CustomerRepository.FetchByAccountNumber(invoice.CustomerAccountNumber); 
       if (customer.InvoiceProperties.InvoiceSequence == InvoiceSequence.OrderReference) 
       { 
        // order by fields at deep level. 
        // invoice.InvoiceLines.OrderBy(i => i.TransactionLines.OfType<PermanentPlacementTransactionLine>()); 
       } 
      } 

我可以公開屬性,以invoiceline,然後進行相應的設置,但有什麼辦法,我可以使用LINQ做到這一點。

+0

看看https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b,你會發現一個排序依據的方法。 –

+0

問題是,無論何時向列表中添加新項目,都希望它們在最後進行,因此列表會停止排序?如果是這樣'SortedList'(https://msdn.microsoft.com/en-us/library/system.collections.sortedlist(v=vs.110).aspx)可能會對你感興趣。否則,我有點不確定你的問題在哪裏。 – Chris

+0

我注意到它看起來只有一些'TransactionLine'對象(那些類型爲'PermanentPlacementTransactionLine')的對象上有OrderReference。你想用其他類型的TransactionLine做什麼?你需要更好地指定這個... – Chris

回答

0

我能夠做到以下方式

 invoiceLines.OrderBy(Func1) 
    //.ThenBy(Func2). 
    //ThenBy(Func3). 
    ToList(); 

private static string Func1(InvoiceLine invoiceLine) => 
      invoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine >().First() 
       .OrderReference ;