将对象集合添加到另一个集合

时间:2016-06-08 15:00:07

标签: c# .net asp.net-mvc

我对c#和.NET MVC很新,很抱歉不知道正确的词汇量。

我有一个API,我从原生Android应用程序调用。

我发送GET请求以获得一些“命题”' (代码是荷兰语,学校说这样做,我不知道......)。

到目前为止一直很好,这是我的存储库功能:

 public IEnumerable<BegrotingsVoorstel> getVoorstelVanProject(int id)
        {
            return ctx.BegrotingsVoorstellen.Where(b => b.ProjectId == id).AsEnumerable();
        }

我得到了这个JSON:

{
    "begrotingsVoorstelId": 1,
    "titel": "Algemeen bespaard",
    "beschrijving": "Voor mij mag er vooral bij algemene financiering bespaard worden.",
    "gebruikerId": 4,
    "projectId": 1
  }

但是现在我想在我回馈的对象中的一个收集字段中添加一个集合(或列表或其他)媒体链接(照片等)。

我期待这种JSON:

{
    "begrotingsVoorstelId": 1,
    "titel": "Algemeen bespaard",
    "beschrijving": "Voor mij mag er vooral bij algemene financiering bespaard worden.",
    "gebruikerId": 4,
    "projectId": 1,
    "begrotingsVoorstelMedia": [{
        "begrotingsVoorstelMediaId": 1,
        "url": "/images/test.jpeg"
      }]
  }

现在这里有一个棘手的部分,begrotingsVoorstelMedia目前是空的,不知道怎么把它弄到里面,Visual Studio不断向我抛出各种错误..

目前,这是我的存储库功能,它不会给出任何错误,但也没有数据......

public IEnumerable<BegrotingsVoorstel> getVoorstelVanProject(int id)
{
    IEnumerable<BegrotingsVoorstel> help = new List<BegrotingsVoorstel>();
    help = ctx.BegrotingsVoorstellen.Where(b => b.ProjectId == id).AsEnumerable();
    foreach(BegrotingsVoorstel voorstel in help)
    {
        voorstel.BegrotingsVoorstelMedia.Concat(ctx.BegrotingsVoorstelMedias.Where(c => c.BegrotingsVoorstelId == voorstel.BegrotingsVoorstelId));
    }
    return help;
}

有关额外信息,请访问我的&#34; BegrotingsVoorstelMedia&#34; Model和BPContext文件:

public class BegrotingsVoorstelMedia
{
    [Key]
    public int BegrotingsVoorstelMediaId { get; set; }
    public string Url { get; set; }
    public int BegrotingsVoorstelId { get; set; }
    public virtual BegrotingsVoorstel BegrotingsVoorstel { get; set; }
}

BPCONTEXT =

 public DbSet<BegrotingsVoorstelMedia> BegrotingsVoorstelMedias { get; set; }

提前感谢!

更新的存储功能

public IEnumerable<BegrotingsVoorstel> getVoorstelVanProject(int id)
{
    IEnumerable<BegrotingsVoorstel> help = new List<BegrotingsVoorstel>();
    List<BegrotingsVoorstelMedia> help2 = new List<BegrotingsVoorstelMedia>();
    help = ctx.BegrotingsVoorstellen.Where(b => b.ProjectId == id).AsEnumerable();
    foreach(BegrotingsVoorstel voorstel in help)
    {
        help2.Clear();
        help2 = ctx.BegrotingsVoorstelMedias.Where(c => c.BegrotingsVoorstelId == voorstel.BegrotingsVoorstelId).ToList();
        foreach(BegrotingsVoorstelMedia voorstelMedia in help2)
        {
            voorstel.BegrotingsVoorstelMedia.Add(voorstelMedia);
        }
    }
    return help;
}

2 个答案:

答案 0 :(得分:2)

您需要调用“AddRange”并将查询转换为List(同时确保您的集合在BegrotingsVoorstel的ctor中新建。我不记得但是ICollection中可能不存在AddRange,所以你只需在每个内部执行另一个循环foreach声明。

voorstel.BegrotingsVoorstelMedia.AddRange(ctx.BegrotingsVoorstelMedias.Where(c => c.BegrotingsVoorstelId == voorstel.BegrotingsVoorstelId).ToList());

答案 1 :(得分:1)

你走在正确的轨道上。坚持LINQ并按照这样做:

public IEnumerable<BegrotingsVoorstel> getVoorstelVanProject(int id)
{
    var help = ctx.BegrotingsVoorstellen.Where(b => b.ProjectId == id);

    foreach(BegrotingsVoorstel voorstel in help)
    {
        var medias = ctx.BegrotingsVoorstelMedias
            .Where(c => c.BegrotingsVoorstelId == voorstel.BegrotingsVoorstelId).ToList(); 
        voorstel.BegrotingsVoorstelMedia = voorstel.BegrotingsVoorstelMedia.Concat(medias);
    }

    return help;
}

请记住,LINQ扩展方法不适用于它们被调用的集合。相反,他们会根据需要返回新的IEnumerable。 LINQ扩展方法的另一个重要方面是它们正在实现“deffered execution”,这意味着您需要在需要时通过迭代集合或使用ToList()ToArray()等来实现结果。

如果你想没有LINQ,你需要在AddRange()初始化后调用medias,如下所示:

voorstel.BegrotingsVoorstelMedia.AddRange(medias);
相关问题