自动映射 - 将平面数据映射到分层数据

时间:2017-01-20 23:03:24

标签: c# .net automapper

假设我的数据集类似于具有父组,第二级组和第三级组的组。让我们说groupId也是独一无二的。鉴于这组数据,我如何使用Automapper的解决方案将这些数据映射到分层表单?

"Groups": [
  {
    "ParentGroupId": "1",
    "ParentGroupName": "Parent Group 1",
    "SecondLevelGroupId": "11",
    "SecondLevelGroupName": "Second Level Group 1",
    "ThirdLevelGroupId": null,
    "ThirdLevelGroupName": null
  },
  {
    "ParentGroupId": "1",
    "ParentGroupName": "Parent 1"
    "SecondLevelGroupId": "12",
    "SecondLevelGroupName": "Second Level Group 2",
    "ThirdLevelGroupId": null,
    "ThirdLevelGroupName": null
  },
  {
    "ParentGroupId": "2",
    "ParentGroupName": "Parent 2"
    "SecondLevelGroupId": "21",
    "SecondLevelGroupName": "Second Level Group 1",
    "ThirdLevelGroupId": "31",
    "ThirdLevelGroupName": "Third Level Group 1"
  },
  {
    "ParentGroupId": "2",
    "ParentGroupName": "Parent 2"
    "SecondLevelGroupId": "21",
    "SecondLevelGroupName": "Second Level Group 1",
    "ThirdLevelGroupId": "32",
    "ThirdLevelGroupName": "Third Level Group 2"
  },
  {
    "ParentGroupId": "2",
    "ParentGroupName": "Parent 2"
    "SecondLevelGroupId": "22",
    "SecondLevelGroupName": "Second Level Group 2",
    "ThirdLevelGroupId": "33",
    "ThirdLevelGroupName": "Third Level Group 1"
  }
]

这是我希望将其映射到的层次结构形式(注意:JSON是手写的,可能存在一些语法错误)

"Groups": [
  {
    "GroupId": "1",
    "GroupName": "Parent Group 1",
    "ChildGroups": [
      {
        "GroupId": "11",
        "GroupName": "Second Level Group 1",
        "ChildGroups": null     
      },
      {
        "GroupId": "12",
        "GroupName": "Second Level Group 2",
        "ChildGroups": null      
      }
    ]
  },
  {
    "GroupId": "2",
    "GroupName": "Parent 2"
    "ChildGroups": [
      {
        "GroupId": "21",
        "GroupName": "Second Level Group 1",
        "ChildGroups": [
          {
            "GroupId": "31",
            "GroupName": "Third Level Group 1",
            "ChildGroups": null
          },
          {
            "GroupId": "32",
            "GroupName": "Third Level Group 2",
            "ChildGroups": null
          }
        ] 
      },
      {
        "GroupId": "22",
        "GroupName": "Second Level Group 2",
        "ChildGroups": [
          {
            "GroupId": "33",
            "GroupName": "Third Level Group 1",
            "ChildGroups": null
          }
        ]
      }
    ]
  }
]

0 个答案:

没有答案