Well I have this:
Board:
public class Board
{
public int BoardID { get; set; }
public string BoardName { get; set; }
public string BoardDescription { get; set; }
public int PostCount { get; set; }
public int TopicCount { get; set; }
public bool IsVisibile { get; set; }
public Forum BelongToForum { get; set; }
public List<Board> Boards { get; set; }
public List<Topic> Topics { get; set; }
}
And Forum:
public class Forum
{
public int ForumID { get; set; }
public string ForumName { get; set; }
public string ForumDescription { get; set; }
public List<Board> Boards { get; set; }
}
And method that get Forums with boards:
public List<Forum> GetForums()
{
List<Forum> forums = new List<Forum>();
_ctx = new ForumContext();
forums = (from p in _ctx.Forums
select p).ToList();
List<Forum> returnForm = new List<Forum>();
foreach(Forum forum in forums)
{
List<Board> board = (from x in _ctx.Boards
where x.BelongToForum.ForumID == forum.ForumID
select x).ToList();
forum.Boards = board;
returnForm.Add(forum);
}
return returnForm;
}
It’s working as expected. But as you see I’m using three lists, and foreach loop. It doesn’t look really good for me. So I have an question, is there another way to make it work ?
I think I should made another model class that will gather nessesary data from join or something like this.
But what I want to ask are there other ways to do it ?
If you look at your
Forumentity it already has aBoardsproperty. You should be able to materialize those in one query usingInclude().Or using the strongly typed
Include():Can you clarify whether this is code first or DB first? From your example it looks like DB first with POCO models (with code first the
Boardsproperty should have been materialized already since it is not marked as virtual).