【问题标题】:Nested Groupby function in Asp.net mvcAsp.net mvc 中的嵌套 Groupby 函数
【发布时间】:2020-11-25 01:25:45
【问题描述】:

我有这个函数可以将 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"]}]

谢谢

【问题讨论】:

  • 您想要的 JSON 无效。在 JSON 中,键值对不能直接在数组内部(用[] 表示);它们必须在对象中(用{} 表示)。有关正确 JSON 构造的信息,请参阅 json.org。您可以使用jsonlint.com 验证您的 JSON。请编辑您的问题以包含正确的 JSON。

标签: c# json group-by asp.net-mvc-5


【解决方案1】:

我假设您的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"
         }
      ]
   }
]

【讨论】:

  • 谢谢你,亲爱的,你救了我的命。
  • @MustafaTaeb 如果这对你有帮助,请接受我的回答,谢谢~
  • 当我将它与您给定的列表一起使用时,我从您的回答中得到了这个想法,它工作正常,但是当我将它与数据库表一起使用时,它只返回一条记录而不是全部,我不知道为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
相关资源
最近更新 更多