1

我的问题是我需要从 dbcontext 查询中呈现以下结构,因为库可以很好地呈现这种格式。这里是json中的数据。

{"Customer":{"Id":"BOLID","CompanyName":"Bólido Comidas preparadas","ContactName":"Martín Sommer","ContactTitle":"Owner","Address":"C/ Araquil, 67","城市":"马德里","邮政编码":"28023","国家":"西班牙","电话":"(91) 555 22 82","传真":"(91) 555 91 99"},"CustomerOrders":[{"Order":{"Id":10326,"CustomerId":"BOLID","EmployeeId":4,"OrderDate":"\/Date(844898400000+0000)\/ ","RequiredDate":"\/Date(847321200000+0000)\/","ShippedDate":"\/Date(845244000000+0000)\/","ShipVia":2,"Freight":77.92,"ShipName “:"Bólido Comidas preparadas","ShipAddress":"C/ Araquil, 67","ShipCity":"Madrid","ShipPostalCode":"28023","ShipCountry":"Spain"},"OrderDetails":[{" OrderId":10326,"ProductId":4,"UnitPrice":17.6,"Quantity":24,"Discount":0},{"OrderId":10326,"ProductId":57,"UnitPrice":15.6," Quantity":16,"Discount":0},{"OrderId":10326,"ProductId":75,"UnitPrice":6.2,"Quantity":50,"Discount":0}]},{"Order" :{"Id":10801,"CustomerId":"BOLID","EmployeeId":4,"OrderDate":"\/Date(883350000000+0000)\/","RequiredDate":"\/Date(885769200000+ 0000)\/","ShippedDate":"\/Date(883522800000+0000)\/","ShipVia":2,"Freight":97.09,"ShipName":"Bólido Comidas preparadas","ShipAddress":"C/ Araquil, 67" ,"ShipCity":"Madrid","ShipPostalCode":"28023","ShipCountry":"Spain"},"OrderDetails":[{"OrderId":10801,"ProductId":17,"UnitPrice":39, "Quantity":40,"Discount":0.25},{"OrderId":10801,"ProductId":29,"UnitPrice":123.79,"Quantity":20,"Discount":0.25}]},{"Order ":{"Id":10970,"CustomerId":"BOLID","EmployeeId":9,"OrderDate":"\/Date(890694000000+0000)\/","RequiredDate":"\/Date(891900000000+0000)\/","ShippedDate":"\/Date(893368800000+0000)\/","ShipVia":1,"Freight":16.16,"ShipName":"Bólido Comidas preparadas" ,"ShipAddress":"C/ Araquil, 67","ShipCity":"Madrid","ShipPostalCode":"28023","ShipCountry":"Spain"},"OrderDetails":[{"OrderId":10970, "ProductId":52,"UnitPrice":7,"Quantity":40,"Discount":0.2}]}]}"ShipCountry":"Spain"},"OrderDetails":[{"OrderId":10970,"ProductId":52,"UnitPrice":7,"Quantity":40,"Discount":0.2}]}]}"ShipCountry":"Spain"},"OrderDetails":[{"OrderId":10970,"ProductId":52,"UnitPrice":7,"Quantity":40,"Discount":0.2}]}]}

哪一个

http://ajaxstack.com/jsonreport/
渲染得非常好。现在我尝试了这个:

控制器的东西

MedicalVarianceEntities DbCtx = new MedicalVarianceEntities();

    var data = 
        (
        from Mvrs in DbCtx.Mvrs
        join MvrMeds in DbCtx.MvrMeds
        on Mvrs.PKMvrId equals MvrMeds.FKMvrId
        join MvrMedsProcurementErrors in DbCtx.MvrMedsProcurementErrors
        on MvrMeds.PKMvrMedsId equals MvrMedsProcurementErrors.FKMvrMedsId
            select new
            {
                MvrId,
                MvrMeds.MvrMedsMedicineName//,
              //  MvrMedsProcurementErrors.MvrMedsProcurementErrorsId
            }
       );

现在的 JQUERY

$(function () {
  $.getJSON("/MedicineManagement/MvrSummary/1", null, function (data) {
                  var test = JSON.stringify(data);
                 alert(test);
});

在此处输入图像描述

请注意,我想要的是这样的 Mvrs":[{"MvrMeds":{"MvrMedsMedicineName":ACETAMINOPHEN}}]

我认为这个 JSON 意味着 Mvrs 是 MvrMeds 的父级,而 MvrMedsMedicineName 是 MvrMeds 的属性。

4

1 回答 1

0

到目前为止,我只能以这种丑陋的方式做到这一点。

     public JsonResult MvrSummary(int MvrId)
        {
       MedicalVarianceEntities DbCtx = new MedicalVarianceEntities();
            var data = from entity in DbCtx.Mvrs
                          select new
                          {
                              Prop1 = entity.PKMvrId,

                              ChildProp = entity.MvrEmployees.Select(x=>x.ODSEmpNumber),
                              GrandChildProp = entity.MvrEmployees.Select(x=>x.MvrEmployeesStorageErrors.Select(y=>y.MvrEmployeesStorageErrorsId))
                          };
 return Json(data, JsonRequestBehavior.AllowGet);
}
于 2012-05-23T23:11:04.550 回答