I\'ve been busy diving into lambda expressions recently, and there\'s some specific functionality that I\'ve been meaning to learn but just couldn\'t seem to make heads or t
You're looking for Action<>
and Func<>
.
You can create extension
public static class MakeArrayExtension
{
public static U[] MakeArray<T, U>(this IEnumerable<T> collection, Func<T,U> func)
{
return collection.Select(func).ToArray();
}
}
And then use it like this
List<A> foo; // assuming this is populated
string[] bar = foo.MakeArray<A,string>(x => x.StringProperty);
public static U[] MakeArray<T, U>(this IEnumerable<T> @enum, Func<T, U> rule)
{
return @enum.Select(rule).ToArray();
}
You can use the Func and Action classes. Check out the LINQ Where function's signature for an example: http://msdn.microsoft.com/en-us/library/bb534803.aspx
Try the following
U[] MakeArray<T,U>(Func<T, U> projection) where T : IEntity {
...
}
When passing lambdas as arguments you generally want the matching parameter to be a delegate. In most cases you should just use the appropriate Action<>
or Func<>
value. The former for void returning lambdas and the latter for ones that return values.