C# 如何将匿名列表从静态方法传递给调用方法?
上面是我开始使用的,但我使用了dictionary func来简化调用并创建其他内容C# 如何将匿名列表从静态方法传递给调用方法?,c#,json,asp.net-mvc,anonymous-types,jsonresult,C#,Json,Asp.net Mvc,Anonymous Types,Jsonresult,上面是我开始使用的,但我使用了dictionary func来简化调用并创建其他内容 public JsonResult GetReport(string reportSelected, string firstDateGiven) { _context = new ReportDB(); var theResults = miPolicyTransactions.Select(
public JsonResult GetReport(string reportSelected, string firstDateGiven)
{
_context = new ReportDB();
var theResults =
miPolicyTransactions.Select(
x =>
new
{
PolicyReference = x.PolicyReference,
TransactionType = x.TransactionType
...
}).ToList();
var theadColumns = new[]
{
new {columnName = "Policy Reference"},
new {columnName = "Transaction Code"}
...
}.ToList();
return Json(new { data = theResults, columns= theadColumns }, JsonRequestBehavior.AllowGet);
}
private Dictionary functions=新字典
{
{“代理最近3个月的新业务(集合)”,新业务代理最近3个月},
{“代理最近7天的新业务(集合)”,SomeOtherMethodName}
};
私有静态JsonResult NewBusinessAgentLast3个月(IReportDB上下文,字符串参数)
{
_context=newreportdb();
结果变量=
miPolicyTransactions。选择(
x=>
新的
{
PolicyReference=x.PolicyReference,
TransactionType=x.TransactionType
...
}).ToList();
var theadColumns=new[]
{
新建{columnName=“策略引用”},
新建{columnName=“事务代码”}
...
}.ToList();
返回??????????????????????????
由于出现错误,我无法返回Json对象
非静态字段、方法、,
属性。无法在静态上下文中访问非静态Json
我是否可以避免创建一个具体类型,每个类型都有一个具体类型列表,但仍然将这两个匿名列表依次传递给调用方法,并作为Jquery文件中使用的JsonResult返回?您是使用列表还是有其他方法???您应该更改函数(如
NewBusinessAgentLast3Month
)要返回对象
。然后应将此值传递给控制器.Json
方法,该方法将创建一个JsonResult
,您可以从控制器返回
代码中的问号应该替换为重构前使用的相同匿名类型。您不能将函数设置为静态的吗?基本上不能,因为这会破坏客户端需要的OOP。我总是将静态方法作为字典。您是说您遇到编译错误或运行时异常吗当您试图使字典中的函数初始化为非statc时,智能和编译错误我使用动态,这本质上是@Martins的建议。非常感谢。
private Dictionary<string, Func<IReportDB, string, JsonResult>> functions = new Dictionary<string, Func<IReportDB, string, JsonResult>>
{
{ "New Business by Agent last 3 Months(set)", NewBusinessAgentLast3Month},
{ "New Business by Agent last 7 days (set)", SomeOtherMethodName}
};
private static JsonResult NewBusinessAgentLast3Month(IReportDB context, string parameters)
{
_context = new ReportDB();
var theResults =
miPolicyTransactions.Select(
x =>
new
{
PolicyReference = x.PolicyReference,
TransactionType = x.TransactionType
...
}).ToList();
var theadColumns = new[]
{
new {columnName = "Policy Reference"},
new {columnName = "Transaction Code"}
...
}.ToList();
return ??????????????????????????