如何以这种特定方式返回数据

时间:2016-03-30 15:38:49

标签: c# json repository-pattern dapper

我正在使用带有dapper micro orm的c#后端构建一个angularjs应用程序,它从数据库中获取数据。

我希望返回的数据如下所示:

WIN_ANSI_ENCODING_TABLE

但这就是我的数据

[{
        "CategoryId": 1,
        "CategoryName": "cat1",
        "Items": [{
            "ItemId": 1,
            "ItemName": "Item1"
        }, {
            "ItemId": 2,
            "ItemName": "Item2"
        }]
    }, {
        "CategoryId": 2,
        "CategoryName": "cat2",
        "Items": [{
            "ItemId": 3,
            "ItemName": "Item3"
        }, {
            "ItemId": 4,
            "ItemName": "Item4"
        }]
    }
]

这就是我在我的存储库中的内容:

[{
        "CategoryId": 1,
        "CategoryName": "cat1",
        "Items": {
            "ItemId": 1,
            "ItemName": "Item1"
        }
    }, {
        "CategoryId": 1,
        "CategoryName": "cat1",
        "Items": {
            "ItemId": 2,
            "ItemName": "Item2"
        }
    },


    {
        "CategoryId": 2,
        "CategoryName": "cat2",
        "Items": {
            "ItemId": 3,
            "ItemName": "Item3"
        }
    },

    {
        "CategoryId": 2,
        "CategoryName": "cat2",
        "Items": {
            "ItemId": 4,
            "ItemName": "Item4"
        }
    }

]

这些是我的类别和项目模型

public IEnumerable<CategoryModel> GetAllCategories()
        {
            using (var conn = ConnectionSettings.GetSqlConnection())
                  {
                      const string sql = @" SELECT
                            c.CategoryName,     
                            c.CategoryId,
                            i.ItemId,
                            i.ItemName,                                
                            i.CategoryId 
                        from Category c
                        INNER JOIN item i ON  c.CategoryId = i.CategoryId";   


    var categoriesList = conn.Query<CategoryModel, ItemModel, CategoryModel>(sql, (cat, it) =>
                              {
                                  cat.Item = it;
                                  return cat;
                              }, splitOn: "ItemId");

                              return categoriesList;


                          }
        }

有人可以指出我正确的方向吗?

请告诉我这里我做错了什么。

提前致谢

1 个答案:

答案 0 :(得分:2)

我建议采用以下方法来实现预期结果:

如下所示修改实体,使用Newtonsoft Json属性忽略CategoryId中的ItemModel,以避免序列化

[JsonObject]
public class CategoryModel
    {

        public int CategoryId { get; set; }

        public string CategoryName { get; set; }

        public IEnumerable<ItemModel> Items { get; set; }

    }

[JsonObject]
public class ItemModel
    {
        public int ItemId { get; set; }

        [JsonIgnore] 
        public int CategoryId { get; set; }

        public string ItemName { get; set; }

    }

现在提取数据,因为Category Model内的一对多映射(包含多个Item Model),使用QueryMultiple分别获取结果集,代码看起来像:

public IEnumerable<CategoryModel> GetAllCategories()
        {
            using (var conn = ConnectionSettings.GetSqlConnection())
                  {
                      const string sql = @" SELECT
                            c.CategoryName,     
                            c.CategoryId from Category c;
                            SELECT
                            i.ItemId,
                            i.ItemName,                                
                            i.CategoryId 
                        from item i";   


    var reader = conn.QueryMultiple(sql);

    IEnumerable<CategoryModel> categoriesList = reader.Read<CategoryModel>();

     IEnumerable<ItemModel> itemList = reader.Read<ItemModel>();


    foreach(Category c in categoriesList)
    {
       c.items = itemList.Where(i => i.CategoryId = c.CategoryId)
    }

    return categoriesList;


                          }
        }