Вложенная функция Groupby в Asp. net mvc - PullRequest
0 голосов
/ 05 августа 2020

У меня есть эта функция, чтобы вернуть данные JSON в мое представление

public JsonResult MeterReading()
    {
        db.Configuration.ProxyCreationEnabled = false;


        var queryNestedGroups =
    from s in db.Sales
    group s by s.PointId into newGroup1
    from newGroup2 in
        (from student in newGroup1
         group student by student.FuelTypeId into g
         select new
         {

             MeterReading = g.Sum(o => o.SaleQuantity),
             FuelType = db.zFuelTypes.Where(u => u.Id == g.Key).Select(p => p.FuelType).FirstOrDefault(),
             Point = db.DispensePoints.Where(u => u.Id == newGroup1.Key).Select(p => p.PointName).FirstOrDefault(),
             Picture=db.DispensePoints.Where(u => u.Id == newGroup1.Key).Select(p => p.Picture).FirstOrDefault()

         })
    group newGroup2 by newGroup1.Key;

        return Json(queryNestedGroups, JsonRequestBehavior.AllowGet);

    }
    

, но она показывает такие данные

[[{"MeterReading":677.00,"FuelType":"Petrol","Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png"},{"MeterReading":677.00,"FuelType":"GasLocal","Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png"}],[{"MeterReading":677.00,"FuelType":"Petrol_95","Point":"www","Picture":"~/Images/PointPicuters/63732163747332.jpg"}}]]

, но я хочу, чтобы они были в этом формате

[{"Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png",["MeterReading":677.00,"FuelType":"Petrol"],["MeterReading":677.00,"FuelType":"GasLocal"]},{"MeterReading":677.00,"FuelType":"Petrol_95",["Point":"www","Picture":"~/Images/PointPicuters/63732163747332.jpg"]}]

Спасибо

1 Ответ

0 голосов
/ 06 августа 2020

Я предполагаю, что ваши db.Sales данные выглядят так:

List<Sales> mySales = new List<Sales>()
{
    new Sales
    {
        Id = 1,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "Petrol",
        SaleQuantity = 100.00M
    },
    new Sales
    {
        Id = 2,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "Petrol",
        SaleQuantity = 200.00M
    },
    new Sales
    {
        Id = 3,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "GasLocal",
        SaleQuantity = 300.00M
    },
    new Sales
    {
        Id = 4,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "GasLocal",
        SaleQuantity = 400.00M
    },
    new Sales
    {
        Id = 5,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "Petrol_95",
        SaleQuantity = 500.00M
    },
    new Sales
    {
        Id = 6,
        PointName = "wwwfhfghfgh",
        Picture = "~/Images/PointPicuters/637321819464806362a.png",
        FullType = "Petrol_95",
        SaleQuantity = 600.00M
    },
    new Sales
    {
        Id = 7,
        PointName = "www",
        Picture = "~/Images/PointPicuters/63732163747332.jpg",
        FullType = "MyNewType",
        SaleQuantity = 10000.00M
    },
    new Sales
    {
        Id = 8,
        PointName = "www",
        Picture = "~/Images/PointPicuters/63732163747332.jpg",
        FullType = "MyNewType",
        SaleQuantity = 20000.00M
    }
};

После 2 групп по операциям

var queryGroupByFullType = from s in mySales
                           group s by s.FullType into newGroup1
                           select new
                           {
                               MeterReading = newGroup1.Sum(x => x.SaleQuantity),
                               PointName = newGroup1.First().PointName,
                               Picture = newGroup1.First().Picture,
                               FullType = newGroup1.Key
                           };

var queryGroypByPointName = from s in queryGroupByFullType
                            group s by s.PointName into newGroup1
                            select new
                            {
                                Point = newGroup1.Key,
                                Picture = newGroup1.First().Picture,
                                MyList = newGroup1.Select(x => new
                                {
                                    x.MeterReading,
                                    x.FullType
                                })
                            };

json:

[
   {
      "point":"wwwfhfghfgh",
      "picture":"~/Images/PointPicuters/637321819464806362a.png",
      "myList":[
         {
            "meterReading":300.00,
            "fullType":"Petrol"
         },
         {
            "meterReading":700.00,
            "fullType":"GasLocal"
         },
         {
            "meterReading":1100.00,
            "fullType":"Petrol_95"
         }
      ]
   },
   {
      "point":"www",
      "picture":"~/Images/PointPicuters/63732163747332.jpg",
      "myList":[
         {
            "meterReading":30000.00,
            "fullType":"MyNewType"
         }
      ]
   }
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...