簡體   English   中英

將對象集合添加到另一個集合

[英]Adding collection of objects to another collection

我是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;
}

有關額外信息,請參閱我的“BegrotingsVoorstelMedia”模型和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;
}

您需要調用“AddRange”並將查詢轉換為List(同時確保您的集合在BegrotingsVoorstel的ctor中新建。我不記得但是ICollection中可能不存在AddRange,因此您只需在每個foreach語句中執行另一個循環。

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

你走在正確的軌道上。 堅持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,你需要在medias初始化之后調用AddRange() ,如下所示:

voorstel.BegrotingsVoorstelMedia.AddRange(medias);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM