I understand that predicates are delegate to function which return bool and take generic parameter, I understand that when I say:
mycustomer => mycustomer.fullname == 1
It actually means:
delegate (Customer mycustomer)
{
return mycustomer.fullName == "John";
}
The paramter I’m passing in when I pass this lambda expression is:
public delegate bool Criteria<T>(T value) which is natively called Predicate
But what I don’t understand is what it means when I say mycustomer=>mycustomer.fullname
In customers.OrderBy(mycustomer=>mycustomer.fullname);
How do I implement something like OrderBy? How do I tell a method which property to do action on ! like the previous example?
By example here is a case I want to make a method which get all values of a collection for a specific property :
list<string> mylist = customers.GetPropertyValues(cus=>cus.Fullname);
Thanks in advance.
The
Func<TElement,TKey>is used to create anIComparer<TKey>which is used internally in anOrderedEnumerableto sort the items. When you do:The
OrderedEnumerabletype is creating anIComparer<TKey>internally. In the above example, ifi.SomePropertywere aStringit would create an instance ofIComparer<String>and then sort the items in the source enumerable using that comprarer on theSomePropertymember.In your last case:
You do this using
Select:Which will return an enumerable of
Stringnames. In theSelectmethod, theFunc<TSource, TResult>is used to select the target element to be added to the result.To replicate this yourself, you could do: