0

我正在尝试使用两个 linq-to-SQL 语句将数据从数据库返回到数组,我可以在 jQuery 调用中使用该数组。我可以让 Json 返回一个数组,但我无法让它返回包含我需要列出的数量的第二个数组。

你看我在做什么,我正在创建一个树视图,列出每个部分的区域、项目和子程序。每个区域都有一个总花费,每个项目都有一个总花费,每个子程序都有一个总花费。我已经能够获得用于子程序的总金额,但项目是另一回事,因为我必须使用用于显示子程序的同一控制器来显示每个项目的总数。它看起来有点像这样。

Area------------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

+Project--------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

++Sub-Program1--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

++Sub-Program2--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

由于项目金额是子程序数量的总和,并且与列出子程序的位置不同,因此我很难填充第二个数组。下面的代码来自我的控制器,显示了我如何为填充子程序名称的数组提取数据。

[Authorize]
        public virtual ActionResult getAjaxPGs(string SP = null, string PG = null)
        {

            if (SP != null)
            {
                var PGList = from x in db.pg_mapping
                             where x.PG_SUB_PROGRAM == SP 
                             select x;
                                 //select x.PG.Distinct().ToArray(); // I wanted to combine the PGRow = line below with the linq to SQL code above but it didn't like the .ToArray() 


                var PGRow = PGList.Select(x => new { x.PG }).Distinct().ToArray();

                return Json(PGRow, JsonRequestBehavior.AllowGet);

            }


            return View();
        }

所以这里的代码找到并输出子程序名称来代替 Sub-Program1 和 Sub-Program2。这部分需要保留,以便它输出正确的子程序名称,但现在我需要添加将在项目级别输出总金额的代码。我以为我可以将此代码添加到控制器并返回两个数组,但我做得不对,而且我尝试过的任何方法都没有奏效。

var AmountList = from x in db.Spend_Web where x.PG == PG group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x => x.Amount) };

然后像这样返回

return Json(PGRow, AmountList, JsonRequestBehavior.AllowGet);

这没有用,它抛出了一个错误,说The best overload match for 'System.Web.MVC.Controller.Json(object,string,System.Web.MVC.JsonRequestBehavior)' has some invalid arguments

我对所有建议完全开放,因为我尝试过的一切都没有奏效;即使一个建议将我带入一个全新的方向。老实说,我只需要一些关于如何实现预期结果的指导。我希望我已经清楚地表达了我的问题。感谢大家的时间和专业知识,请不要犹豫,要求任何额外的代码或澄清,因为我会尽可能经常地检查。

更新

public class Container
        {
            Array PARow;
            Array AmountList;

        }

public virtual ActionResult getAjaxPGs(string SP = null, string PG = null)
            {
                var PGList = from x in db.pg_mapping
                             where x.PG_SUB_PROGRAM == SP
                             select x;

                container.PGRow = PGList.Select(x => new { x.PG }).Distinct().ToArray();

                container.AmountList = from x in db.Spend_Web
                                    where x.PG == PG
                                    group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup
                                    select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x => x.Amount) };


            }
4

1 回答 1

1

创建一个“容器”类,其中包含您希望返回的每种数据类型的实例。

class Container
{
    Array PGRow;
    Array AmountList;
}

然后在您的代码中:

Container container = new Container();
container.PGRow = ....;
container.AmountList = ....;
于 2012-08-27T17:23:26.057 回答