I've constructed a database and used the Entity Framework to generate some domain model classes. Basically VideCollection
is a collection of Videos and I'd like to display the each Video Collection along with it's Videos on some page. Following the repository pattern I've made a repository for the video collection. I can pull the VideoCollection objects from the database. The problem is I need to 'fix' the objects to add their corresponding videos. Is this the correct way of doing it? If so then I need to 'fix' the videos to bring in their comments etc etc causing a huge chain. I understand about LINQ deferred queries but this seems unnecessary to pull so much information out of the database. I'm using ASP.NET MVC3.
If anyone can point me in the right direction it would be much appreciated!
using System;
using System.Linq;
using VideoCart.Domain.Abstract;
using VideoCart.Domain.Entities;
namespace VideoCart.Domain.Concrete
{
public class EFVideoRepository : IVideoRepository
{
private readonly EFDbContext _context = new EFDbContext();
public IQueryable<VideoCollection> GetVideoCollections()
{
IQueryable<VideoCollection> videoCollections = _context.VideoCollections;
foreach (var videoCollection in videoCollections)
开发者_JAVA技巧{
videoCollection.Videos = _context.Videos.Where(x => x.VideoCollectionId == videoCollection.Id);
}
return videoCollections;
}
}
}
I'm not sure if this is what you want, but, you can use the include method to specify the related objects you want to pull out and include with the query results.
精彩评论