Skip to main content
 首页 » 编程设计

c#之将对象集合添加到另一个集合

2024年04月18日35telwanggs

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

我有一个从 native Android 应用程序调用的 API。

我正在发送一个 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());