LINQ - LINQ Query Error - Asked By Bob on 08-Sep-11 06:32 PM

I am using EF 4 for my asp.net MVC project, and I have a query to return all photos in my repository as follows:

public IQueryable<Photo> ListAllPhotos()
      {       
        var photoList = (from albumPhoto in entities.AlbumPhotoes
               join photo in entities.Photos on albumPhoto.PhotoId equals photo.PhotoId
               join photoAlbum in entities.PhotoAlbums on albumPhoto.PhotoAlbumId equals photoAlbum.PhotoAlbumId
               join photoCategory in entities.PhotoCategories on photo.PhotoCategoryId equals photoCategory.PhotoCategoryId              
               select new Photo
               {
                 photo.PhotoId,
                 photo.PhotoTitle,
                 photo.PhotoDesc,
                 photo.PhotoCaption,
                 photo.PhotoFileUrlThumb,
                 photo.PhotoViewCount,
                 photo.CreationDate,
                 photoCategory.PhotoCategoryName,
                 photoAlbum.PhotoAlbumName
               });
        return photoList; //error here,not compile!
      }

I need to return the query results which includes multiple records so that they can be displayed in a view (through a viewmodel). but as shown in above code, the return statement has an error saying that "

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQuerayable<Model.PhotoGallery.Photo>'.

How to fix that? Please help

More general question is how to return the query results in IQuerayble.

Riley K replied to Bob on 08-Sep-11 10:03 PM
Try to return AsQueryable

return contacts.ToList().AsQueryable();

Regards
TSN ... replied to Bob on 08-Sep-11 11:45 PM
hi..

As you mentioned in your Method signature the return type should match with type you defined and value you are returning .....

you specified in signature as IQueryable<photo> but in method you are returning a var remove the var and replace it with IQueryable<photo>

or try like this...

var photoList = (from albumPhoto in entities.AlbumPhotoes
         join photo in entities.Photos on albumPhoto.PhotoId equals photo.PhotoId
         join photoAlbum in entities.PhotoAlbums on albumPhoto.PhotoAlbumId equals photoAlbum.PhotoAlbumId
         join photoCategory in entities.PhotoCategories on photo.PhotoCategoryId equals photoCategory.PhotoCategoryId        
         select new Photo
         {
         photo.PhotoId,
         photo.PhotoTitle,
         photo.PhotoDesc,
         photo.PhotoCaption,
         photo.PhotoFileUrlThumb,
         photo.PhotoViewCount,
         photo.CreationDate,
         photoCategory.PhotoCategoryName,
         photoAlbum.PhotoAlbumName
         }).


return photoList.AsQueryable<photo>();
TSN ... replied to Bob on 08-Sep-11 11:47 PM
you can try like this to

public IQueryable<Photo> ListAllPhotos()
 {   
return  (from albumPhoto in entities.AlbumPhotoes
 join photo in entities.Photos on albumPhoto.PhotoId equals photo.PhotoId
 join photoAlbum in entities.PhotoAlbums on albumPhoto.PhotoAlbumId equals photoAlbum.PhotoAlbumId
 join photoCategory in entities.PhotoCategories on photo.PhotoCategoryId equals photoCategory.PhotoCategoryId       
select new Photo
 {
    photo.PhotoId,
    photo.PhotoTitle,
    photo.PhotoDesc,
    photo.PhotoCaption,
    photo.PhotoFileUrlThumb,
    photo.PhotoViewCount,
    photo.CreationDate,
    photoCategory.PhotoCategoryName,
    photoAlbum.PhotoAlbumName
   });
}


hope this helps you....
Bob replied to TSN ... on 09-Sep-11 02:04 PM
None of these suggestions works.

I googled a while, but not found anything explicitly show you how to return IQueryable results to MVC controller.