2013-01-04 10 views
9

Ho una query LINQ che restituisce risultati che corrispondono alla mia classe PictureGallery. Ho bisogno di caricarli nei miei ViewModel ma im ottenendo il seguente errore:Come caricare questi risultati LINQ nella classe ViewModel?

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Sono piuttosto nuovo in C#. Come lancio "Risultati" nella classe "viewMddel" di PictureGallery?

Grazie in anticipo!

Controller:

//Test MediaID 
var MediaID = 75; 

//Query Results 
var Results = from g in DB.Galleries 
       join m in DB.Media on g.GalleryID equals m.GalleryID 
       where g.GalleryID == GalleryID 
       orderby m.MediaDate descending, m.MediaID descending 
       select new { g.GalleryTitle, Media = m }; 

//Create my viewmodel 
var Model = new GalleryViewModel 
{ 
    MediaID = MediaID, 
    PictureGallery = Results, //This line throws the error. 
    PictureCount = Results.Count() 
}; 

ViewModels:

public class GalleryViewModel 
{ 
    public int? MediaID { get; set; } 
    public IEnumerable<PictureGallery> PictureGallery { get; set; } 
    public int PictureCount { get; set; } 
} 

public class PictureGallery 
{ 
    public int GalleryID { get; set; } 
    public string GalleryTitle { get; set; } 
    public int MediaID { get; set; } 
    public string MediaTitle { get; set; } 
    public string MediaDesc { get; set; } 
    public double Rating { get; set; } 
    public int Views { get; set; } 
} 

risposta

16

riformulare la query come:

//Query Results 
var Results = from g in DB.Galleries 
       join m in DB.Media on g.GalleryID equals m.GalleryID 
       where g.GalleryID == GalleryID 
       orderby m.MediaDate descending, m.MediaID descending 
       select new PictureGallery { 
           GalleryID = g.GalleryId, 
           GalleryTitle = g.GalleryTitle, 
           MediaID = m.MediaID, 
           MediaTitle = m.MediaTitle, 
           MediaDesc = m.MediaDesc, 
           Rating = m.Rating, 
           Views = m.Views} ; 
+0

Yup che ha funzionato! Grazie – Maddhacker24

2

Si sta tentando di impostare un IEnumerable<PictureGallery> a un IQueryable<anonymous>. È necessario per trasformare il tipo corretto:

var Model = new GalleryViewModel 
{ 
    MediaID = MediaID, 
    PictureGallery = Results 
     .Select(r => new PictureGallery { 
      GalleryID = r.Media.GalleryID, 
      GalleryTitle = r.GalleryTitle, 
      MediaID = r.Media.MediaID, 
      ... // and so on 
     }), 
    PictureCount = Results.Count() 
}; 
0

allora si può scrivere lista var = result.ToList();

Quindi passare a visualizzare in questo modo visualizzazione di ritorno (elenco);

si può accettare a suo avviso come questo elenco @model

Dove ViewModel è una semplice classe con le proprietà per accettare i valori risultanti dopo l'esecuzione di query.

È possibile controllare questi allegati [query LINQ, vista del modello, Vista enter image description here][1]

Problemi correlati