Often when you try to create a general list operator, you end up redefining what LINQ already offers.
Here's the LINQ code for what you need (without the AwesomeHelper function):
var results = list.Select(l => new MyResult() { UserId = l.UserId, ResultValue = l.SomeDecimalValue.ToString() }).ToList();
Pretty simple.
If you want to have the AwesomeHelper function for your request, it looks like this:
public static List<MyResult> AwesomeHelper( List<SomeBigExternalDTO> input, Func<SomeBigExternalDTO, object> selector) { return input .Select(i => new MyResult() { UserId = i.UserId, ResultValue = selector(i).ToString() }) .ToList(); }
And the calling code looks like this:
var results = AwesomeHelper(list, x => x.SomeIntValue);
For me, this is now less than the LINQ option. Now there is some magic, and itβs hard to understand that.
I have an alternative that will give you the best of both worlds.
First, define an extension method called ToMyResult that maps one instance of SomeBigExternalDTO to one MyResult with a field selector, for example:
public static class AwesomeHelperEx { public static MyResult ToMyResult( this SomeBigExternalDTO input, Func<SomeBigExternalDTO, object> selector) { return new MyResult() { UserId = input.UserId, ResultValue = selector(input).ToString() }; } }
Now the calling code is crystal clear, flexible and concise. There he is:
var results = ( from item in list select item.ToMyResult(x => x.SomeLongValue) ).ToList();
Hope this helps.