我有一個方法與此類似,其循環通過一組數據,並使用第一對象的值來查找一個對象中的第二組數據:動態功能/ lambda表達式
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();
...
}
}
我想,使其更重用通過傳遞函數功能,或者使用一些其它類型的拉姆達的,然後方法轉換的東西,我可以在一個通用的方式使用,即:
private void someMethod<SourceT, TargetT>(IQueryable<SourceT> source, IQueryable<TargetT> targetData) {
....
}
我是什麼不完全確定的是我可以如何構建一個Func /謂詞/等,並將其傳遞給方法。請記住,所有SourceT屬性中的「id」屬性都不相同。
爲了進一步解釋,我想的東西在那裏我可以做到這一點:
的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'
);
我的源和目標IQueryable數據不是來自同一個數據源,事實上,它甚至不是數據庫 - 它來自Web服務。如何調用someMethod外觀(P代表什麼?) – bugfixr
@Chu - 由於它們來自不同的源,因此不需要使用'IQueryable <>'和表達式。我仍然使用'GroupJoin'方法並儘可能保持你的'foreach'儘可能輕。 'P'只是你加入的屬性(或者更確切地說它們的類型)。 – Enigmativity
感謝您的澄清。只是爲了理解選擇器部分,我將什麼傳遞給someMethod作爲我的'sourceSelector'? – bugfixr