简体   繁体   English

如何根据 C# 中列表中的列表值对列表进行排序?

[英]How to Sort a List based on the value of a list within the list in C#?

Probably the question is a bit confusing but basically I have a list which inside consisting another list.可能这个问题有点令人困惑,但基本上我有一个列表,其中包含另一个列表。 Like this:像这样:

public class Transaction
{
    public string TransactionType { get; set; }
    public List<TransactionDetails> Details { get; set; } = new List<TransactionDetails>();
}

public class TransactionDetails
{
    public string TransactionDate { get; set; }
    public double TransactionAmount { get; set; }
    public double TransactionBalance { get; set; }
}

Now, I want to sort the Transaction list based on the TransactionDate inside the TransactionDetails list.现在,我想根据TransactionDetails 列表中的 TransactionDate 对 Transaction列表进行排序。 If let's say I just want to sort it based on TransactionType , I know that this is the way to do it: (assuming I already added the list)如果假设我只想根据TransactionType对其进行排序,我知道这是这样做的方法:(假设我已经添加了列表)

    List<Transaction> listTransactions;
    List<Transaction> sortedListTransactions;

    sortedListTransactions = listTransactions
        .OrderBy(x => x.TransactionType)
        .ToList();

I tried it and this worked.我试过了,这很有效。 But, when I applied basically the same method for my case,which is to sort the list based on TransactionDate , it doesnt give me the answer I needed.但是,当我对我的案例应用基本相同的方法时,即基于TransactionDate对列表进行排序时,它并没有给我所需的答案。 Please let me know if there is any way I could achieve this..请让我知道是否有任何方法可以实现这一目标..

If you have to sort a list, you have to pick an element which defines the order (like a value, a date, etc.) If the property you like to use for sorting is also a list, you have to either pick one element from that inner list (eg min, max, first, last) or to aggregate all inner list values (like sum, average, median, concat) to one value.如果您必须对列表进行排序,则必须选择一个定义顺序的元素(如值、日期等)。如果您喜欢用于排序的属性也是列表,则必须选择一个元素从该内部列表(例如 min、max、first、last)或将所有内部列表值(如 sum、average、median、concat)聚合为一个值。

One approach in your case could be to find the minimum element within the inner list and use this as criteria for sorting the outer elements.在您的情况下,一种方法可能是在内部列表中找到最小元素,并将其用作对外部元素进行排序的标准。 This could be something like this:这可能是这样的:

var sortedTransactions = transactions
        .OrderBy(transaction => transaction.Details
                                    .OrderBy(detail => detail.TransactionDate)
                                    .FirstOrDefault())
        .ToList();

From your example another possibility to sort the outer elements from the inner list, could be to take the sum of amount of all transactions, which would be something like this:从您的示例中,从内部列表中对外部元素进行排序的另一种可能性是获取所有交易金额的总和,如下所示:

var sortedTransactions = transactions
        .OrderBy(transaction => transaction.Details
                                    .Sum(detail => detail.TransactionAmount))
        .ToList();

Nevertheless, it is up to you to define the criteria of sorting on every level and then let it bubble up to give the sorting criteria back.尽管如此,您可以定义每个级别的排序标准,然后让它冒泡以返回排序标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM