【发布时间】:2017-02-15 01:36:43
【问题描述】:
我有一个类似这样的方法,它遍历一组数据并使用第一个对象的值在第二组数据中找到一个对象:
private void someMethod(IQueryable<RemoteUser> source, IQueryable<LocalUser> targetData) {
// Loop all records in source data
foreach(var u in source) {
// Get keyvalue from source data and use it to find the matching record in targetData
var keyValue = u.id;
var object = from data.Where(o => o.id == keyValue).FirstOrDefault();
...
}
}
我想通过传入 Func 或使用其他类型的 lambda 使其更可重用,然后将该方法转换为我可以以通用方式使用的方法,即:
private void someMethod<SourceT, TargetT>(IQueryable<SourceT> source, IQueryable<TargetT> targetData) {
....
}
我不太确定如何构建 Func/Predicate/etc 并将其传递给方法。请记住,“id”属性在所有 SourceT 和 TargetT 属性中都不相同。
为了进一步解释,我想要一些可以做到这一点的东西:
someMethod(RemoteUsers, LocalUsers, something here to say 'find the user using the userId property');
someMethod(RemoteProducts, LocalProducts, something here to say 'find the user using the productId property');
【问题讨论】: