如下的代码:
public async Task<ActionResult> GetPostCountInCategories(int[] categoryIds) { var categories = await categoryService .GetCategoriesWithPostCount(categoryIds) .Select(c => new { c.CategoryId, c.PostCount }) .ToDictionaryAsync(k => k.CategoryId, v => v.PostCount); return Json(categories); }
运行时出现下面的错误:
字典的序列化/反序列化不支持类型“System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”,键必须为字符串或对象。
请问如何解决这个问题?
.ToDictionaryAsync(k => k.CategoryId.ToString(), v => v.PostCount);