I have a collection of courses (Id, name, status, instructor, etc) and another collection of Pricing (CourseId, Price).
I am trying to write a linq extension method to merge these two together, but my reflection is a bit rusty. What am I missing?
This is how I want to call it:
courses.SewData(pricing, p => p.Id, c => c.CourseId);
This is the method that is being called. Im having issues with the ToDictionary Line. Its not compiling. How can I create a dictionary with my existing expression
public static class DataExtension {
public static IEnumerable<TParent> SewData<TParent, TChild>(this IList<TParent> parentCollection, IList<TChild> childCollection, Expression<Func<TParent, object>> parentProperty, Expression<Func<TChild, object>> childProperty) {
var parentPropertyInfo = ReflectionHelper.GetProperty(parentProperty);
var childPropertyInfo = ReflectionHelper.GetProperty(childProperty);
var parentDict = parentCollection.ToDictionary(parentProperty, x => x); //need help here
foreach (var child in childCollection) {
var childId = childPropertyInfo.GetValue(child);
var parentItem = parentDict[childId];
//use reflection here to map properties by name
yield return parentItem;
}
}
}
Aucun commentaire:
Enregistrer un commentaire