Gửi bởi
nguyen930411
Theo mình biết thì(sai thì rep lại giúp mình nha =.=):
+ Trong OrderBy là hàm vô danh, bạn nghiên cứu thêm về "biểu thức lambda", "anonymous method" để biết thêm.
+ Trước dấu "=>" là tham số.
+ Sau dấu "=>" là nội dung hàm, đáng lẽ có dấu {} nhưng do chỉ 1 dòng lệnh nên không cần.
+ Regex.Match(s.Name, @"(\d*)\.html").Groups[1].Value -> phần Regex nếu bạn chưa biết thì search thêm nhé, group[1] là lấy vị trí (\d*)...group[1].value là lấy giá trị ra.
+ ThenBy được dùng sau OrderBy, là điều kiện sort thứ 2.
+ Mặc định OrderBy là sort tăng dần, nếu bạn muốn giảm dần thì thêm dấu "-" trước Int32.Parse.
Kiến thức mình còn hổng nhiều lắm, bạn xem thêm trên mạng nhé: https://msdn.microsoft.com/library/bb534966(v=vs.100).aspx
Có gì add fb mình sẽ nói chi tiết hơn:
[email protected]
Vài chổ của bạn @nguyen930411 sai nhé.
1/. OrderBy KHÔNG phải là hàm vô danh (not an anonymous method). Đó là extension method (tiếng Việt là hàm mở rộng, nhưng theo mình thì nên hiểu theo nghĩa extension, đừng dịch ra làm gì).
Kiểu trả về (returned type) của OrderBy là IOrderedEnumerable<TSource>, được declare trong class Enumerable. Cụ thể:
Mã:
public static class Enumerable{ // // Summary: // Sorts the elements of a sequence in ascending order according to a key. // // Parameters: // source: // A sequence of values to order. // // keySelector: // A function to extract a key from an element. // // Type parameters: // TSource: // The type of the elements of source. // // TKey: // The type of the key returned by keySelector. // // Returns: // An System.Linq.IOrderedEnumerable`1 whose elements are sorted according to a // key. // // Exceptions: // T:System.ArgumentNullException: // source or keySelector is null. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);}
Extension method có 2 loại: 1 loại chấp nhận parameter và 1 loại không chấp nhận parameter.
Ví du:
Mã:
// none parametervar item = list.First(); // Lấy item đầu tiên trong list // one parametervar _item = list.First(x => x == "abc"); // Lấy item đầu tiên trong các item mà có giá trị (value) là abc
2/. OrderBy vì không là anonymous method nên không thể viết theo cú pháp: list.OrderBy(x => { x }). Chổ này sao bạn không check thử trước khi up bài nhỉ?
Đúng là anonymous method có chấp nhận cú pháp đó, nhưng OrderBy không là anonymous method.
3/. Nếu nói theo cách của anonymous method thì câu khẳng định: Trước dấu "=>" là tham số là không chính xác.
Anonymous method có thể có nhiều hơn 1 tham số, hoặc không.
Ví dụ:
Mã:
Action action = () =>{ // anonymous method with none argument Console.WriteLine("Hello World!");}; // hoặc đơn giản hơn là:Action _action = () => Console.WriteLine("Hello World!"); Action<string> action1 = x =>{ // anonymous method with 1 argument Console.WriteLine(x);}; Action<int, int> action2 = (x, y) =>{ // anonymous method with 2 arguments Console.WriteLine(x + y);};
Ngoài ra, bạn còn có thể declare 1 extension method tương tự như OrderBy để dùng:
Mã:
public static IOrderedEnumerable<TSource> AnotherOrderByMe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector){ //do stuff...}
Áp dụng cho topic này:
Mã:
public static class MyExtensions{ public static IEnumerable<string> OrderByMe(this IEnumerable<string> list) { int maxLen = list.Select(s => s.Length).Max(); return list.Select(s => new { OrgStr = s, SortStr = Regex.Replace(s, @"(\d+)|(\D+)", m => m.Value.PadLeft(maxLen, char.IsDigit(m.Value[0]) ? ' ' : '\xffff')) }) .OrderBy(x => x.SortStr) .Select(x => x.OrgStr); }} public class MyClass{ public static void Main(string[] args) { var files = new List<string>(Directory.EnumerateFiles(@"F:\Download\images\", "*.txt")); files.OrderByMe().ToList().ForEach(x => Console.WriteLine(x)); Console.ReadLine(); }}
Ghi chú: Từ .Net version 4.0 trở về sau, bạn có thể dùng EnumerateFiles thay cho GetFiles nhé.
Thân!
Dưới ánh nắng mặt trời đang lên, những đống pallet nhựa An Giang trải dài mênh mông như những bức tranh tự nhiên đầy sắc màu. Mỗi chiếc pallet nhựa không chỉ là một vật dụng thông thường mà còn là...
Pallet nhựa An Giang giao hàng...