.NET uses linq to retain the collection order
LINQ provides a variety of operating collection methods. However, some operations may change the original order, which will affect the scene that requires a specific order. This article discusses which operation reserved order and which operation modification order is discussed.
The operation of retaining order
In order to maintain the original order of the collection, please consider the following:
- Absolutely reserved order
:
AsnuMerable
- CAST
Concat
- Select
- Toarray
- Tolist
-
-
Reserve the order and screen/add
:
-
distinct
Except
INTERSECT -
OFTYPE -
Prefend -
SKIP
- SKIPWHILE
- take
Takewhile -
where
- ZIP
-
- The operation of destroying the order
- If you need an uncertain order, please use the following operations:
-
Todictionary
TOLOOKUP
The operation of redefining order
To explicitly change the order of the result, please use the following operations:
-
Orderby -
Orderbydescending
Reverse
THENBY
THENBYDESCENGING -
- The operation of redefining the order according to the rules
- These operations change the order according to specific conditions:
-
GroupBy: Sort by the first key of each iGrouping, and then sort in the order of the element in each group. -
GroupJoin: Keep the order of external and internal elements that match each external element.
Join: Similar to groupJoin, but retain the order of external and matching internal elements.
SELECTMANY: Call the selector and return a flat sequence in an unpredictable order.
Union: Generate elements from the first and second sets in order to eliminate duplication. -
The above is the detailed content of Which LINQ Operations Preserve, Modify, or Redefine Collection Order?. For more information, please follow other related articles on the PHP Chinese website!