我如何通过功能委托给被调用LINQ? [英] How do I pass function delegates to be called with LINQ?
问题描述
我想我提出正确的问题在这里...
I think I'm asking the right question here...
我有一个返回相同数据的不同子集4的存储过程。
I have 4 stored procedures that return different subsets of the same data.
我映射在我的服务器应用程序这一数据同一个对象。
I map this data to the same object in my server application.
我已经把我的code,如下所示:
I've set my code up as follows:
internal static MyDataContext dc = new MyDataContext();
internal static List<MyObj> getData(DataType data)
{
List<MyObj> obj = null;
switch (data)
{
case DataType.Type1:
obj = mapObj(dc.getDataType1);
break;
case DateType.Type2:
obj = mapObj(dc.getDataType2);
break;
...
}
}
// This gives me an error that type T cannot be defined
// private static List<MyObj> mapObj(Func<T> getDataForObj)
// This gives me an error calling the function (Cannot find implementation of query pattern for source type T
private static List<MyObj> mapObj<T>(Func<T> getDataForObj)
{
List<MyObj> obj = new List<MyObj>();
var result = from a in getDataForObj()
select a;
foreach (var row in result)
{
... // map objs
}
return obj;
}
请参阅我的意见就方法声明为我遇到的问题。我将如何做到这一点是否正确?我的目标是只是不具有相同的code复制/粘贴多次......试图遵循DRY原则。在此先感谢您的帮助。
Please see my comments regarding the method declaration for the issues I'm having. How would I accomplish this correctly? My goal was to just not have the same code copy/pasted multiple times...trying to follow DRY principles. Thanks in advance for your help.
推荐答案
让我mapObj功能如下(在评论中每@乔恩飞碟双向的建议),允许它来编译和运行:
Making my mapObj function as follows (Per @Jon Skeet's recommendation in the comments) allowed it to compile and run:
private static List<MyObj> mapObj<T>(Func<IEnumerable<T>> getDataForObj)
{
List<MyObj> obj = new List<MyObj>();
var result = from a in getDataForObj()
select a;
foreach (var row in result)
{
... // map objs
}
return obj;
}
这导致了有关领域的映射一个新的问题,但我会问,在一个新的问题。
This led to a new problem regarding the mapping of the fields, but I will ask that in a new question.
这篇关于我如何通过功能委托给被调用LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!