如何将匿名列表传递给静态方法的调用方法?

How do I pass anonymous lists to calling method from static method?

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<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 ??????????????????????????

我无法 return 一个 Json 对象,因为我得到一个错误

An object reference is required for the non static field, method, property. Cannot access non-static Json in static context.

我能否避免为每个创建一个具有具体类型列表的具体类型,但仍然将两个匿名列表传递给调用方法,然后由 returned 作为 JsonResult 传递,这是在我的 Jquery 文件中使用?您会使用 List 还是有其他方法??

您应该将函数(如 NewBusinessAgentLast3Month)更改为 return object。然后,您应该将此值传递给 Controller.Json 方法,该方法将创建一个 JsonResult,您可以从控制器 return。

代码中的问号应替换为重构前使用的相同匿名类型。