Say I have two tables in SQL that are related, photos table and a phototags table which are joined on photoid, where there is one to many relation.
I want to return a list of photos where each photo has a property called called tags that is list.
This is how I solved it, but I wonder if there is a better way of doing this? One where there is a single trip to the database.
I want to return a list of photos where each photo has a property called called tags that is list.
This is how I solved it, but I wonder if there is a better way of doing this? One where there is a single trip to the database.
Code:
public List<Photo> GetAllPhotos()
{
List<PhotoBO> photoListBO= _photoAppRepository.GetAllPhotos().ToList();
List<TagBO> listTagBO = _photoAppRepository.GetAllTags().ToList();
List<Photo> list = new List<Photo>();
foreach (PhotoBO p in photoListBO)
{
Photo photo = _autoMapperAdapter.Map<Photo>(p);
List<string> listTags = new List<string>();
foreach (TagBO t in listTagBO)
{
if (p.PhotoID == t.PhotoID)
{
listTags.Add(t.TagName);
}
}
photo.PhotoTags = listTags;
list.Add(photo);
}
return list;
}