Linq in plain English

Can someone explain in the simple English syntax of this:

Here is the signature of the operator OrderBy:

OrderedSequence<TElement> OrderBy<TElement, TKey>(
    this IEnumerable<TElement> source, 
    Func<TElement, TKey> keySelector
)

This shows that the type of delegate that needs to be provided for OrderByis Func<TElement, TKey>.

I am looking to create a function that receives a list and a string as a parameter (column name), and I am stuck in the syntax of the extension method OrderBy. What does it mean Func<...>? Where can I put a string parameter to sort with?

Thank.

+3
source share
4 answers

Here you can see it in practice, given

List<MyClass> list;

and

public class MyClass
{
    public string Name { get; set; }
    // ...
}

you can say

list.OrderBy(x => x.Name);

this IEnumerable<TElement> source is how we know that we call it as an extension method by any IEnumerable.

, . , , . , , :

public object GetPropertyByName(object obj, string propertyName)
{
    object result = null;

    var prop = obj.GetType().GetProperty(propertyName);
    result = prop.GetValue(obj, null);

    return result;

}

:

List<MyClass> list = new List<MyClass>();
list.Add(new MyClass { Name = "John" });
list.Add(new MyClass { Name = "David" });
list.Add(new MyClass { Name = "Adam" });
list.Add(new MyClass { Name = "Barry" });

const string desiredProperty = "Name"; // You can pass this in
var result = list.OrderBy(x => GetPropertyByName(x, desiredProperty));
foreach (MyClass c in result)
{
    Console.WriteLine(c.Name);
}
+2

, - ( ), IEnumerable (TElement). , , .

: http://msdn.microsoft.com/en-us/library/bb534966.aspx

        class Pet
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public static void OrderByEx1()
        {
            Pet[] pets = { new Pet { Name="Barley", Age=8 },
                           new Pet { Name="Boots", Age=4 },
                           new Pet { Name="Whiskers", Age=1 } };

            IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);

            foreach (Pet pet in query)
            {
                Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
            }
        }

        /*
         This code produces the following output:

         Whiskers - 1
         Boots - 4
         Barley - 8
        */
+2

Func<TElement, TKey> , TElement TKey. (, foo.OrderBy<MyObject, string>(...)), .

OrderBy , , , . ,

list.OrderBy(item => item.SortByField)

, - , ( , ), .

, , , . - , Expression , Func.

( - - )

using System.ComponentModel;

public static IEnumerable<TValue> OrderByField<TValue>(this IEnumerable<TValue> source, string fieldName) {
    PropertyDescriptor desc = TypeDescriptor.GetProperties(typeof(TValue))[fieldName];
    return source.OrderBy(item => desc.GetValue(item));
}
+1

U

(from o in listOfObjects
select o).OrderBy(p => p.columnname)

Here the column name is the column by which you want to order

+1
source

Source: https://habr.com/ru/post/1789617/


All Articles