Reputation: 897
I'm fairly new to c# and .NET MVC, so sorry for not knowing the correct vocabulary.
I have an API that I call from a native Android application.
I'm sending a GET request to get a number of 'propositions' (code is in dutch, school said to do so, I know otherwise...).
So far so good, here's my repository function:
public IEnumerable<BegrotingsVoorstel> getVoorstelVanProject(int id)
{
return ctx.BegrotingsVoorstellen.Where(b => b.ProjectId == id).AsEnumerable();
}
I get this JSON back:
{
"begrotingsVoorstelId": 1,
"titel": "Algemeen bespaard",
"beschrijving": "Voor mij mag er vooral bij algemene financiering bespaard worden.",
"gebruikerId": 4,
"projectId": 1
}
But now I want to add a collection (or list or whatever) of media links (photo's) into a collection field in the objects I'm giving back.
I'm expecting this kind of 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"
}]
}
Now here's the tricky part, begrotingsVoorstelMedia is currently empty, no idea how to get it inside, Visual Studio keeps throwing various errors at me..
For now, this is my repository function, it doesn't give any errors, but also no data...
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;
}
For extra information, here's my "BegrotingsVoorstelMedia" Model and BPContext file:
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; }
thanks in advance!
UPDATED REPOSITORY FUNCTION
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;
}
Upvotes: 1
Views: 1312
Reputation: 3634
You're on the right track. Stick to LINQ and do it like this:
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;
}
Keep in mind that the LINQ extension methods DON'T work against the collection they're invoked on. Instead they return new IEnumerable as appropriate. Another important aspect of LINQ extension methods is that they're implementing "deffered execution", meaning you need to materialize the result when needed by iterating over the collection or use ToList()
, ToArray()
, etc.
If you want to go without LINQ, you need to call AddRange()
after medias
initialization, like so:
voorstel.BegrotingsVoorstelMedia.AddRange(medias);
Upvotes: 1
Reputation: 2241
You need to call "AddRange" and convert the query to List (also make sure your collection is newed up in BegrotingsVoorstel's ctor. I don't recall but AddRange may not exist in ICollection, so you just do another loop inside each foreach statement.
voorstel.BegrotingsVoorstelMedia.AddRange(ctx.BegrotingsVoorstelMedias.Where(c => c.BegrotingsVoorstelId == voorstel.BegrotingsVoorstelId).ToList());
Upvotes: 2