C# 将通用Linq扩展方法创建为;sew";按键排列的数据
我有一套课程(Id、姓名、状态、讲师等)和另一套定价(CourseId、Price) 我正试图编写一个linq扩展方法来将这两种方法合并在一起,但我的反射有点生疏。我错过了什么 我想这样称呼它:C# 将通用Linq扩展方法创建为;sew";按键排列的数据,c#,linq,reflection,C#,Linq,Reflection,我有一套课程(Id、姓名、状态、讲师等)和另一套定价(CourseId、Price) 我正试图编写一个linq扩展方法来将这两种方法合并在一起,但我的反射有点生疏。我错过了什么 我想这样称呼它: courses.SewData(pricing, p => p.Id, c => c.CourseId); 这就是正在调用的方法。我对ToDictionary系列有意见。它不是编译。如何使用现有表达式创建词典 public static class DataExtension {
courses.SewData(pricing, p => p.Id, c => c.CourseId);
这就是正在调用的方法。我对ToDictionary系列有意见。它不是编译。如何使用现有表达式创建词典
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;
}
}
}
公共静态类数据扩展{
公共静态IEnumerable数据(此IList parentCollection、IList childCollection、Expression parentProperty、Expression childProperty){
var parentPropertyInfo=ReflectionHelper.GetProperty(parentProperty);
var childPropertyInfo=ReflectionHelper.GetProperty(childProperty);
var parentDict=parentCollection.ToDictionary(parentProperty,x=>x);//此处需要帮助
foreach(childCollection中的var child){
var childId=childPropertyInfo.GetValue(child);
var parentItem=parentDict[childId];
//在此处使用反射按名称映射属性
收益率项目;
}
}
}
我想说,这部分不需要表达式和反射,您可以尝试类似的方法(还为键类型添加了TKey
通用参数):
公共静态类数据扩展{
公共静态IEnumerable数据(
这是我的亲子收藏,
IList儿童收藏,
Func parentKeySelector,
Func(儿童钥匙选择器){
var parentDict=parentCollection.ToDictionary(parentKeySelector);
foreach(childCollection中的var child){
var childId=childKeySelector(child);
var parentItem=parentDict[childId];
//在此处使用反射按名称映射属性
收益率项目;
}
}
}
如果您以后出于其他原因仍然需要表达式,您可以使用
Compile
从parentProperty
获取parentKeySelector
,我想说,您不需要表达式和反射,您可以尝试类似的方法(还为键类型添加了TKey
通用参数):
公共静态类数据扩展{
公共静态IEnumerable数据(
这是我的亲子收藏,
IList儿童收藏,
Func parentKeySelector,
Func(儿童钥匙选择器){
var parentDict=parentCollection.ToDictionary(parentKeySelector);
foreach(childCollection中的var child){
var childId=childKeySelector(child);
var parentItem=parentDict[childId];
//在此处使用反射按名称映射属性
收益率项目;
}
}
}
如果以后出于其他原因仍然需要表达式,您可以使用
Compile
从parentProperty
获取parentKeySelector
,您能给出一个您试图实现的输入和输出的示例吗?我认为这里有一个简单的解决方案,但是很难从您的描述和代码中准确地说出您想要实现的是什么您能举一个您试图实现的输入和输出的示例吗?我认为这里有一个简单的解决方案,但很难从您的描述和代码中准确地说出您想要的是什么achieve@ChrisKooken我很乐意帮忙@克里斯库肯很乐意帮忙!
public static class DataExtension {
public static IEnumerable<TParent> SewData<TParent, TChild, TKey>(
this IList<TParent> parentCollection,
IList<TChild> childCollection,
Func<TParent, TKey> parentKeySelector,
Func<TChild, TKey> childKeySelector) {
var parentDict = parentCollection.ToDictionary(parentKeySelector);
foreach (var child in childCollection) {
var childId = childKeySelector(child);
var parentItem = parentDict[childId];
//use reflection here to map properties by name
yield return parentItem;
}
}
}