This is related to my question at Fun (?) with Linq Expressions in extension methods but this is more of an abstraction of a piece I'm trying to understand. I'm really having difficulty wrapping my brain around this concept.
Given an object defined as such:
public class item
{
public int itemId { get; set; }
public string itemName { get; set; }
public string itemDescription { get; set; }
}
and an IEnumerable<item> foo
,
let's say I want to write an extension method such that the expression
foo.GetFullDescription(x => x.itemId.ToString() + "(" + x.itemName + ")")
would equal an IEnumerable<string>
containing the concatenated items as defined in the Lambda expression. For example, if my foo
object contained exactly one item
object like so:
{
fooItem.itemId = 1
fooItem.itemName = "foo"
fooItem.itemDescription = "fooDescription"
}
... and I assigned a variable to the result of the extension method like so:
var bar = foo.GetFullDescription(x => x.itemId.ToString() + "(" + x.itemName + ")");
... I would get bar
to be an IEnumerable<string>
with one item in it, and that item would equal 1(foo)
.
How would I write that extension method? The first part is relatively straightforward:
public static IEnumerable<string> GetFullDescription(this IEnumerable<item> target, <some expression?> expr){
}
Some help would be appreciated. Simple explanations would be very appreciated.