How can I pass a field (via lambda expression) into a method and then use that field as part of a linq query?
I would like to call the method something like
IDictionary<string, string> stuff = foo(items, otherItems, otherItems => otherItems.FieldToUse)
I'm not sure how I would write the method, but I would want to use it sort of like the code below. I know I could use generics and pass the field name (via a string) into the method, but even then I don't know how I would use it in the linq query as follows. Also, I like using lambda, since I could just rename the field anytime I choose.
private IDictionary<string, string> foo<TModel>(IEnumerable<string> items, IEnumerable<TModel> otherItems, object FieldToUse)
{
//this will return a list of key value pairs of rowIDs and equipment
IDictionary<string, string> x = (from o in otherItems
join i in items on o.FieldToUse.ToString() equals i //joining on the equipment assetcode
select new { rowID = o.RowID, item = i }).ToDictionary(k => k.rowID.ToString(), v => v.item);
return x;
}
Clarification: FieldToUse is a property or field of TModel