【问题标题】:ManyToMany relation - downloading data in JSON多对多关系 - 以 JSON 格式下载数据
【发布时间】:2019-02-17 00:03:34
【问题描述】:

我在表之间有一个多对多关系:

  • 用户
  • 标签

(一切都按照建议进行)

public IEnumerable<User> GetAll()
{
    return _context.Users
        .Include(x => x.UserTags)
            .ThenInclude(z => z.Tag);
}

应用很深

[
    {
        "id": 1,
        "username": "Jon",
        "password": null,
        "userTags": [
            {
                "userId": 1,
                "user": {
                 ...

我想得到:

[
    {
        "id": 1,
        "username": "Bartek",
        "password": null,
        "userTags": [ // List of Tags ]

已编辑

目前我已经做了如下

[HttpGet]
public IActionResult GetAll()
{
    var users = _userService.GetAll();

    List<UserDto> result = new List<UserDto>();

    foreach (var user in users)
    {
        var tagDto = _mapper.Map<IList<TagDto>>(user.UserTags.Select(x => x.Tag));

        var userDtos = new UserDto
        {
            Id = user.Id,
            Username = user.Username,
            Tags = tagDto
        };
        result.Add(userDtos);
    }

    return Ok(result);
}

但是没有任何内置选项可以达到预期的效果吗?

【问题讨论】:

    标签: asp.net-core .net-core asp.net-core-2.0 asp.net-core-webapi


    【解决方案1】:

    我为这种情况构建了自己的 Mapper:

    public class MyMapper
    {
        public IList<UserDto> GetUserDto(IEnumerable<User> users, IMapper _mapper)
        {
            List<UserDto> result = new List<UserDto>();
    
            foreach (var user in users)
            {
                var tagDto = _mapper.Map<IList<TagDto>>(user.UserTags.Select(x => x.Tag));
    
                var userDtos = new UserDto
                {
                    Id = user.Id,
                    Username = user.Username,
                    Tags = tagDto
                };
                result.Add(userDtos);
            }
    
            return result;
        }
    }
    

    使用:(控制器)

    private IMapper _mapper;
    private MyMapper _myMapper;
    
    public UsersController(IMapper mapper)
    {
        _mapper = mapper;
        _curioMapper = new CurioMapper();
    }
    
    [HttpGet]
    public IActionResult GetAll()
    {
        var users = _userService.GetAll();
        var result = _myMapper.GetUserDto(users, _mapper);
    
        return Ok(result);
    }
    

    【讨论】:

      【解决方案2】:

      只是一个想法,假设“一切都按照建议完成”

      public IEnumerable<User> GetAll()
      {
          return _context.Users
              .Include(x => x.UserTags)
                  .ThenInclude(z => z.Tag);
      }
      

      这个数据似乎表明

      [
          {
              "id": 1,
              "username": "Bartek",
              "password": null,
              "userTags": [ // List of Tags ]
      

      你应该改成这个

      public IEnumerable<User> GetAll()
      {
          return _context.Users
              .Include(a => a.UserTags.Tags.Select(c => c.tagname));
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 1970-01-01
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        相关资源
        最近更新 更多