【问题标题】:How do I load these LINQ results into my ViewModel class?如何将这些 LINQ 结果加载到我的 ViewModel 类中?
【发布时间】:2012-12-19 10:17:37
【问题描述】:

我有一个 LINQ 查询,它返回与我的 PictureGallery 类匹配的结果。我需要将它们加载到我的ViewModel 但我收到以下错误:

不能隐式转换类型 'System.Linq.IQueryable' 到 'System.Collections.Generic.IEnumerable'。 存在显式转换(您是否缺少演员表?)

我是 C# 的新手。如何将“结果”投射到我的“PictureGallery”viewmddel 类中?

提前致谢!

控制器:

//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()
};

视图模型:

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; }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 linq


    【解决方案1】:

    然后你可以写 var list = result.ToList();

    然后像这样传递给视图 返回视图(列表);

    您可以像这样在您的视图中接受它 @模型列表

    其中 ViewModel 是一个简单的类,具有在查询执行后接受结果值的属性。

    可以查看这些附件[Linq query,View Model,Viewenter image description here][1]

    【讨论】:

      【解决方案2】:

      将您的查询改写为:

      //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} ;
      

      【讨论】:

        【解决方案3】:

        您正在尝试将IEnumerable&lt;PictureGallery&gt; 设置为IQueryable&lt;anonymous&gt;。您需要转换为正确的类型:

        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()
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-08
          • 1970-01-01
          • 2020-02-15
          • 1970-01-01
          • 1970-01-01
          • 2021-04-16
          • 1970-01-01
          相关资源
          最近更新 更多