gpt4 book ai didi

c# - 如何传递要使用 LINQ 调用的函数委托(delegate)?

转载 作者:行者123 更新时间:2023-11-30 18:03:50 25 4
gpt4 key购买 nike

<分区>

我想我在这里问的是正确的问题......

我有 4 个存储过程,它们返回相同数据的不同子集。

我将此数据映射到我的服务器应用程序中的同一对象。

我的代码设置如下:

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;
}

请查看我对我遇到的问题的方法声明的评论。我将如何正确完成此操作?我的目标是不要多次复制/粘贴相同的代码……试图遵循 DRY 原则。预先感谢您的帮助。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com