linq to sql type error RRS feed

  • Question

  • Hi! I am getting this error:
    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<VideoDataAccess.VideoVideoSections>'. An explicit conversion exists (are you missing a cast?) 
    And here is my code:


    public class VideoVideoRepository : IVideoVideoSectionRepository<VideoVideoSections>



    private VideoBusinessEntitiesDataContext db;



    public IEnumerable<VideoVideoSections> getAll()



    var ldrVideos = (from s in db.VideoSections


    join h in db.Videos on s.VideoSectionID equals h.VideoSectionID


    select new { s.VideoSectionID, s.VideoSectionName, h.VideoID, h.VideoThumbnailImage });


    return ldrVideos.ToList();


    And this is the IVideoVideoRepository


    public interface IVideoVideoSectionRepository<T> where T: class



    IEnumerable<T> getAll();


    Friday, April 10, 2009 11:31 PM


  • The problem is that you are doing a "select new" to create a new anonymous type and then trying to enumerate it as if it was a VideoVideoSection.

    If VideoVideoSelection is one of the types in the query then return it directly with something like select s.

    If it is a special class to contain the fields you list in the new (i.e. the section name AND the video name) then do select new VideoVideoSection() { VideoSectionID = s.VideoSectionID...

    Alternatively setup a relationship between the VideoSections and Videos so you don't need to do a join, eager-load the sections with loadwith and then just access and return Video to the caller so that if they want sections they can go via the navigation property without any additional database hits.

    Saturday, April 11, 2009 5:35 PM