错误地解析了ReturnJSON方法

时间:2014-12-10 01:39:03

标签: c# asp.net-mvc json linq

此方法

var fListItems = db.FListItems.Include(f => f.FList)
                              .Include(f => f.Item)
                              .GroupBy(g => new { g.Item.Name, g.FList.Title })
                              .Select(lg =>
                               new FListItemsViewModel
                               {
                                   Title = lg.Key.Title,
                                   ItemStat = new ItemStat
                                   {
                                       Name = lg.Key.Name,
                                       NameCount = lg.Count(),
                                       NameSum = lg.Sum(w => w.Score),
                                       NameAverage = lg.Average(w => w.Score)
                                   }
                               });

返回此JSON

[
    {
        Title: "Animals",
        ItemStat: {
            Name: "Cat",
            NameCount: 2,
            NameSum: 8,
            NameAverage: 4
        }
    },
    {
        Title: "Animals",
        ItemStat: {
            Name: "Dog",
            NameCount: 1,
            NameSum: 5,
            NameAverage: 5
        }
    }
]

但我希望它能返回这个JSON:

[
    {
        Title: "Animals",
        ItemStats: [
            {
                Name: "Cat",
                NameCount: 2,
                NameSum: 8,
                NameAverage: 4
            },
            {
                Name: "Dog",
                NameCount: 1,
                NameSum: 5,
                NameAverage: 5
            }
        ]
    }
]

有人可以建议怎么做吗?这是viewmodel:

public class FListItemsViewModel
{
    public string Title { get; set; }
    public ItemStat ItemStat { get; set; }
}
public class ItemStat
{
    public string Name { get; set; }
    public int NameCount { get; set; }
    public int NameSum { get; set; }
    public double NameAverage { get; set; }
}

而不是GroupBy TitleName在同一条款中,用两个单独的GroupBy s重写我的查询会更好吗?

基于Rahul回答的进展:

[
    {
        Title: "Animals",
        ItemStat: [
            {
                Name: "Cat",
                NameCount: 2,
                NameSum: 8,
                NameAverage: 4
            },
            {
                Name: "Cat",
                NameCount: 2,
                NameSum: 8,
                NameAverage: 4
            }
        ]
    },
    {
        Title: "Animals",
        ItemStat: [
            {
                Name: "Dog",
                NameCount: 1,
                NameSum: 5,
                NameAverage: 5
            }
        ]
    }
]

2 个答案:

答案 0 :(得分:0)

首先,您需要将ViewModel更改为::

public class FListItemsViewModel
{
    public string Title { get; set; }
    public List<ItemStat> ItemStat { get; set; }
}

然后您可以以可枚举格式访问结果::

var fListItems = db.FListItems.Include(f => f.FList)
                              .Include(f => f.Item)
                              .GroupBy(g => new { g.Item.Name, g.FList.Title })
                              .Select(lg =>
                               new FListItemsViewModel
                               {
                                   Title = lg.Key.Title,
                                   ItemStat =lg.Select(x=> new ItemStat
                                   {
                                       Name = x.Key.Name,
                                       NameCount = x.Count(),
                                       NameSum = x.Sum(w => w.Score),
                                       NameAverage = x.Average(w => w.Score)
                                   }).ToList()
                               });

答案 1 :(得分:0)

这会产生我之后的输出所以我想说这就是答案。

除非有人能发现任何不妥之处吗?

 var fListItems = db.FListItems.Include(f => f.FList)
                               .Include(f => f.Item)
                               .GroupBy(g => g.FaveList.Title)
                               .Select(lg =>
                                   new FListItemsViewModel
                                       {
                                           Title = lg.Key,
                                           ItemStat = lg.GroupBy(x => x.Item.Name)
                                                        .Select(x => new ItemStat
                                                            {
                                                                Name = x.Key,
                                                                NameCount = x.Count(),
                                                                NameSum = x.Sum(w => w.Score),
                                                                NameAverage = x.Average(w => w.Score)
                                                            }).ToList()
                                            });