【发布时间】:2021-08-16 18:03:05
【问题描述】:
我正在使用实体框架工作,我从我的 Get 方法中得到以下 json。
[
{
"id": 2,
"name": "Coffee",
"icon": "alarm",
"staticEvents": [
{
"id": 36,
"categoryId": 2,
"time": "2021-12-26T12:22:46.609+10:00",
"comment": null
},
{
"id": 38,
"categoryId": 2,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
},
{
"id": 40,
"categoryId": 2,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
},
{
"id": 42,
"categoryId": 2,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
},
{
"id": 44,
"categoryId": 2,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
}
]
},
{
"id": 3,
"name": "Water",
"icon": "alert",
"staticEvents": [
{
"id": 32,
"categoryId": 3,
"time": "2021-05-28T14:33:47.765643+10:00",
"comment": null
},
{
"id": 37,
"categoryId": 3,
"time": "2021-05-20T13:00:58.123+10:00",
"comment": null
},
{
"id": 39,
"categoryId": 3,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
},
{
"id": 41,
"categoryId": 3,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
},
{
"id": 43,
"categoryId": 3,
"time": "2021-05-28T00:00:00+10:00",
"comment": null
}
]
}
]
我需要以下格式的东西:
[
{
"month": June,
"report": [
{
"categoryId": 2,
"occurrences": 5,
},
{
"categoryId": 3,
"occurrences": 4,
}
}
]
我想这与 this thread 中的 GroupBy + Select 类似,但我无法获得子列表中的出现次数。
编辑:我正在尝试逐步运行查询,但出现错误。
- 当我跑步时:
var list = _context.Categories
.SelectMany(c => c.StaticEvents )
.GroupBy (staticEvent => new
{
Year = staticEvent.Time.Year,
Month = staticEvent.Time.Month
})
.ToList();
foreach(StaticEvent i in list) {
Console.WriteLine(i.CategoryId);
}
我明白了
System.InvalidOperationException: Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side.
所以我用了:
var list = _context.Categories
.SelectMany(c => c.StaticEvents )
.AsEnumerable()
.GroupBy (staticEvent => new
{
Year = staticEvent.Time.Year,
Month = staticEvent.Time.Month
})
.ToList();
然后我得到了
System.InvalidCastException: Unable to cast object of type 'System.Linq.Grouping`2[<>f__AnonymousType2`2[System.Int32,System.Int32],api.Entities.StaticEvent]' to type 'api.Entities.StaticEvent'.
【问题讨论】:
-
那是你拥有的 json。在代表该结构的类中反序列化它,然后选择你需要的并序列化它。