Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射基于嵌套值重新映射对象_C#_Asp.net_Arrays_Json_Automapper - Fatal编程技术网

C# 自动映射基于嵌套值重新映射对象

C# 自动映射基于嵌套值重新映射对象,c#,asp.net,arrays,json,automapper,C#,Asp.net,Arrays,Json,Automapper,我目前正在从数据库中获取一个对象列表,并希望通过Automapper以某种方式从我的ASP.NETWebAPI返回它。从数据库中获取对象后,我的对象当前看起来像这样: [ { "id": 1, "dropdown": "Country", "value": "Germany" }, { "id": 2, "dropdown": "Country", "value": "Unit

我目前正在从数据库中获取一个对象列表,并希望通过Automapper以某种方式从我的ASP.NETWebAPI返回它。从数据库中获取对象后,我的对象当前看起来像这样:

[
    {
        "id": 1,
        "dropdown": "Country",
        "value": "Germany"
    },
    {
        "id": 2,
        "dropdown": "Country",
        "value": "United States"
    },
    {
        "id": 5,
        "dropdown": "Type",
        "value": "Lead"
    },
    {
        "id": 6,
        "dropdown": "Type",
        "value": "Account"
    },
    {
        "id": 7,
        "dropdown": "Type",
        "value": "Reseller"
    },
    {
        "id": 8,
        "dropdown": "Type",
        "value": "Distributor"
    }
]
但我希望它看起来像这样:

[{
    "Countries": [{
        "id": 1,
        "value": "Germany"
      },
      {
        "id": 2,
        "value": "United States"
      }
    ]
  },
  {
    "Type": [{
        "id": 5,
        "value": "Lead"
      },
      {
        "id": 6,
        "value": "Account"
      }
    ]
  }
]
    public async Task<IEnumerable<DropdownValue>> GetDropdownValues(string[] dropdowns)
    {
        var dropdownValues = _context.DropdownValues.OrderBy(x => x.Id).ThenBy(x => x.Dropdown).AsQueryable();

        if (dropdowns.Length != 0 || dropdowns != null)
        {
            dropdownValues = dropdownValues.Where(x => dropdowns.Contains(x.Dropdown));
        }

        var dropdownValuesToReturn = await dropdownValues.ToListAsync();

        return dropdownValuesToReturn;
    }
//Your result class should actually look like this to match JSON:
class DropDownListDto
{
    public string Description {get;set;}
    //here you need an IEnumerable to store the list of Ids and values associated
    //with this drop-down.  you could create a class and declare a LIST<> of that
    //but I'll just use a dictionary instead.
    public Dictionary<int, string> DropDownValues{get;set;}
}

//Get source data
var source = await GetDropDownValues(<insert [] here>);

//transform list into IEnumerable<DropDownListDto>
var result = source
    //if we GROUP BY the Dropdown property then we'll get a distinct list of
    //of drop-downs.
    .GroupBy(x=>x.DropDown)
    //can then get the list of values and Ids from the resulting groups
    .Select(g=>new DropDownListDto
         {
              //key is the grouped by thing - drop down name..
              Description = g.Key,
              //doing a .select from the group result gives the elements
              //in that group only.  Get an anonymous type and cast that
              //to our required dictionary type
              DropDownValues = g
                .Select(x=>new{ key=x.Id, value = x.Value})
                .ToDictionary(k=>k.key, v=>v.value)
         });
当前我的CreateMap看起来像这样

CreateMap<DropdownValue, DropdownValueListDto>();
public class DropdownValueListDto
{
    public int Id { get; set; }
    public string Dropdown { get; set; }
    public string Value { get; set; }
}
我的LINQ操作如下所示:

[{
    "Countries": [{
        "id": 1,
        "value": "Germany"
      },
      {
        "id": 2,
        "value": "United States"
      }
    ]
  },
  {
    "Type": [{
        "id": 5,
        "value": "Lead"
      },
      {
        "id": 6,
        "value": "Account"
      }
    ]
  }
]
    public async Task<IEnumerable<DropdownValue>> GetDropdownValues(string[] dropdowns)
    {
        var dropdownValues = _context.DropdownValues.OrderBy(x => x.Id).ThenBy(x => x.Dropdown).AsQueryable();

        if (dropdowns.Length != 0 || dropdowns != null)
        {
            dropdownValues = dropdownValues.Where(x => dropdowns.Contains(x.Dropdown));
        }

        var dropdownValuesToReturn = await dropdownValues.ToListAsync();

        return dropdownValuesToReturn;
    }
//Your result class should actually look like this to match JSON:
class DropDownListDto
{
    public string Description {get;set;}
    //here you need an IEnumerable to store the list of Ids and values associated
    //with this drop-down.  you could create a class and declare a LIST<> of that
    //but I'll just use a dictionary instead.
    public Dictionary<int, string> DropDownValues{get;set;}
}

//Get source data
var source = await GetDropDownValues(<insert [] here>);

//transform list into IEnumerable<DropDownListDto>
var result = source
    //if we GROUP BY the Dropdown property then we'll get a distinct list of
    //of drop-downs.
    .GroupBy(x=>x.DropDown)
    //can then get the list of values and Ids from the resulting groups
    .Select(g=>new DropDownListDto
         {
              //key is the grouped by thing - drop down name..
              Description = g.Key,
              //doing a .select from the group result gives the elements
              //in that group only.  Get an anonymous type and cast that
              //to our required dictionary type
              DropDownValues = g
                .Select(x=>new{ key=x.Id, value = x.Value})
                .ToDictionary(k=>k.key, v=>v.value)
         });
公共异步任务GetDropdownValues(字符串[]下拉列表) { var dropdownValues=_context.dropdownValues.OrderBy(x=>x.Id).ThenBy(x=>x.Dropdown.AsQueryable(); if(dropdowns.Length!=0 | | dropdowns!=null) { dropdownValues=dropdownValues.Where(x=>dropdowns.Contains(x.Dropdown)); } var dropdownValuesToReturn=等待dropdownValues.ToListSync(); 返回dropdownValuesToReturn; } 如果有人能帮我做到这一点,那就太好了


提前感谢

根据您的编辑,我想您必须这样做:

[{
    "Countries": [{
        "id": 1,
        "value": "Germany"
      },
      {
        "id": 2,
        "value": "United States"
      }
    ]
  },
  {
    "Type": [{
        "id": 5,
        "value": "Lead"
      },
      {
        "id": 6,
        "value": "Account"
      }
    ]
  }
]
    public async Task<IEnumerable<DropdownValue>> GetDropdownValues(string[] dropdowns)
    {
        var dropdownValues = _context.DropdownValues.OrderBy(x => x.Id).ThenBy(x => x.Dropdown).AsQueryable();

        if (dropdowns.Length != 0 || dropdowns != null)
        {
            dropdownValues = dropdownValues.Where(x => dropdowns.Contains(x.Dropdown));
        }

        var dropdownValuesToReturn = await dropdownValues.ToListAsync();

        return dropdownValuesToReturn;
    }
//Your result class should actually look like this to match JSON:
class DropDownListDto
{
    public string Description {get;set;}
    //here you need an IEnumerable to store the list of Ids and values associated
    //with this drop-down.  you could create a class and declare a LIST<> of that
    //but I'll just use a dictionary instead.
    public Dictionary<int, string> DropDownValues{get;set;}
}

//Get source data
var source = await GetDropDownValues(<insert [] here>);

//transform list into IEnumerable<DropDownListDto>
var result = source
    //if we GROUP BY the Dropdown property then we'll get a distinct list of
    //of drop-downs.
    .GroupBy(x=>x.DropDown)
    //can then get the list of values and Ids from the resulting groups
    .Select(g=>new DropDownListDto
         {
              //key is the grouped by thing - drop down name..
              Description = g.Key,
              //doing a .select from the group result gives the elements
              //in that group only.  Get an anonymous type and cast that
              //to our required dictionary type
              DropDownValues = g
                .Select(x=>new{ key=x.Id, value = x.Value})
                .ToDictionary(k=>k.key, v=>v.value)
         });
//为了匹配JSON,结果类实际上应该如下所示:
类DropDownListDto
{
公共字符串说明{get;set;}
//这里需要一个IEnumerable来存储ID和关联值的列表
//使用此下拉列表。您可以创建一个类并声明该类的列表
//但我会用字典来代替。
公共字典下拉值{get;set;}
}
//获取源数据
var source=等待GetDropDownValues();
//将列表转换为IEnumerable
var结果=源
//如果我们按下拉属性分组,那么我们将得到一个不同的
//下拉列表。
.GroupBy(x=>x.下拉列表)
//然后可以从结果组中获取值和ID的列表
.选择(g=>new DropDownListDto
{
//键是按事物分组的下拉列表名称。。
描述=g.键,
//从组结果中进行选择将得到元素
//仅在该组中。获取匿名类型并强制转换该类型
//到我们所需的字典类型
下拉值=g
.Select(x=>new{key=x.Id,value=x.value})
.ToDictionary(k=>k.key,v=>v.value)
});

这应该会给你一些你想要的东西-虽然我没有测试过…

这里你不想要一个简单的映射,这更像是一个转换操作。-您可以为此使用linq to对象。我在上面插入了用于linq操作的代码。如何使用LINQ to对象?显示数据外观的JSON与
DropDownValueListDto
对象不匹配。。。JSON的东西更像你期望看到的吗?是的,JSON的东西更像我期望看到的。我试图修改DropDownValueListD以匹配我希望看到它的方式,但没有成功。好的,我将一起给出答案。。。。给我一个mo…@EE-没问题。今天早上我意识到,我没有对声明
result
为新的
列表的行执行任何操作,因此我删除了它,并添加了一条注释,使得
result
变量实际上是一个
IEnumerable
。如果需要实际的
列表
类型,您可以使用
.ToList()
来强制转换该类型,但它可能会按原样执行。