0

We are using nTiers and I have this list of objects that i need to sort by a property but then by another property,i have the following but i cant figure out how to to the ThenBy, I have seen examples with List<object> but not with TList<object>.

TList<Client> clients = service.GetClientsById(cid);
clients.Sort("ClientOrder");

but then i need to sort it by the ClientName. I saw an example in another post using LINQ, but i cant get this to work like that (example below),which would allow me to do a ThenBy

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

Instead of downloading and compiling git yourself, you may use MacPorts or HomeBrew to do that. Even if you don't want to use one of them, you may take a look on the formula used to install git to see how it could be done.

4

2 に答える 2

1

ThenBy<Client> 呼び出す前に呼び出す必要がありますToList<Client>:

List<Order> sortedList =
    clients.OrderBy(o => o.ClientOrder)
           .ThenBy(o => o.ClientName)
           .ToList();

ThenBy<T>sでのみ機能しますIOrderedEnumerable<T>sIOrderedEnumerable<T>は からの戻り値の型OrderBy<T>ですが、 を呼び出すとToList<T>、それはIOrderedEnumerable<T>もはや ではなく、List<T>List<T>実装しませんIOrderedEnumerable<T>

于 2013-07-25T00:46:24.097 に答える
1

ComparisonクラスをList.Sort次のように使用できます。

clients.Sort(new Comparison<Client>(
    (client1, client2) =>
    {
        // compare dates
        int result = client1.OrderDate.CompareTo(client2.OrderDate);
        // if dates are equal (result = 0)
        if(result == 0)
            // return the comparison of client names
            return client1.ClientName.CompareTo(client2.ClientName);
        else
            // otherwise return dates comparison
            return result;
    })
);

(上記の例では、null 値の処理はスキップされますが、実際の作業には含まれている必要があります)。

于 2013-07-25T00:51:39.280 に答える