在另一个Resful api列表中显示一个列表。

时间:2017-08-25 11:25:01

标签: c# linq

我有一个restful api,它返回一个图片列表和一个评论列表,但我看不到评论列表,我的服务只返回一个空列表。

这是我的回报

api/getjoin/{id:int}

{
    "idPicture": 1,
    "iduser": 15,
    "picture": "adress image",
    "comments": [],
    "likes": [],
    "users": null
  },

我想展示一系列评论和我的喜欢。喜欢这个

{
    "idPicture": 1,
    "iduser": 15,
    "picture": "adress image",
    "comments": {
                  "idcomment":1,
                   "comment": bla bla bla;
                },
                {
                  "idcomment":2,
                   "comment": bla bla bla;
                },
                {
                  "idcomment":3,
                   "comment": bla bla bla;
                },

    "likes": {
                  "amount":3;
                },
    "users": null
  },

这是我在c#

中的get class
// GET api/getallpictures by user
        [HttpGet]
        [Route("api/getallpictures/{id:int}")]
        public List<pictures> Getallpictures(int id)
        {
            List<pictures> pictureList = new List<pictures>();
            List<comments> comemnts = new List<comments>();
            List<likes> listlikes = new List<likes>();
            var pic = from pictures in dc.pictures
                       where pictures.iduser == id
                      select pictures;
            foreach (var item in pic)
            {
                pictures pc = new pictures();
                pc.iduser = item.iduser;
                pc.idPicture = item.idPicture;
                pc.picture = item.picture;
                pictureList.Add(pc);

            };
            return pictureList;

        }

这是我的模特课

public class pictures { 
                       public int? idPicture { get; set; } 
                       public string iduser { get; set; } 
                       public string picture { get; set; } 
                   } 

public class comments { 
                       public int? idcomments { get; set; } 
                       public string idPicture { get; set; }
                       public string comment { get; set; } 
                      } 
public class likes { 
                    public int? idlikes { get; set; } 
                    public string idPicture { get; set; } 
                    public string amount { get; set; } 
                   } 

1 个答案:

答案 0 :(得分:0)

首先你需要像这样更新图片类。

def patient_params
  params.require(:patient).permit(:firstName, :surname, bloodgroup_id)
end

现在图片类与评论和喜欢有关系。 现在在foreach循环中,您可以执行以下操作,例如

    public class pictures
    {
        public int? idPicture { get; set; }
        public string iduser { get; set; }
        public string picture { get; set; }
        public List<comments> comments { get; set; }
        public List<likes> likes { get; set; }
    }

将数据添加到对象中。