无法隐式转换类型'System.Linq.IQueryable< AnonymousType#1>' [英] Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'

查看:2083
本文介绍了无法隐式转换类型'System.Linq.IQueryable< AnonymousType#1>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到我的C#项目的错误,导致我头疼。该错误是:

 无法隐式转换类型'System.Linq.IQueryable< AnonymousType#1>'
到System.Collections.Generic.IEnumerable< KST.ViewModels.Gallery>'。

下面是我的LINQ querys:

  //获取单个图片
VAR PictureResults =(从DB.Media m其中m.MediaID ==选择450米).SingleOrDefault();//获取画廊的图片和标题画廊
从DB.Galleries摹VAR GalleryResults =
                     加入米DB.Media上g.GalleryID等于m.GalleryID成克
                     其中,g.GalleryID == 100
                     选择新的{g.GalleryTitle,媒体=克};

下面是我的ViewModels。

 公共类GalleryViewModel
{
    大众媒体媒体{搞定;组; }
    公共IEnumerable的<画廊>画廊{搞定;组; }
}公共类图库
{
    公共字符串GalleryTitle {搞定;组; }
    公众诠释MediaID {搞定;组; }
    公众诠释GalleryID {搞定;组; }
    公共字符串MediaGenre {搞定;组; }
    公共字符串MediaTitle {搞定;组; }
    公共字符串MediaDesc {搞定;组; }
}

下GalleryResults发生的squigally行错误:

  //创建视图模型我
VAR模型=新GalleryViewModel
{
    媒体= PictureResults,
    画廊= GalleryResults
};


解决方案

UfukHacıoğulları已发布一个答案,并删除了它,几分钟后。我觉得他的回答是正确的,他的解决办法摆脱的错误信息。所以我再次张贴:

UfukHacıoğulları的答案;

您的投影匿名类型,而不是图库的序列。只是实例化对象画廊在SELECT语句中,它应该工作。

  VAR GalleryResults从DB.Galleries G =
                     加入米DB.Media上g.GalleryID等于m.GalleryID成克
                     其中,g.GalleryID == 100
                     选择新的画廊{GalleryTitle = g.GalleryTitle,媒体=克};

Im getting an error in my C# project which is causing me a headache. The error is:

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
to System.Collections.Generic.IEnumerable<KST.ViewModels.Gallery>'.

Here's my LINQ querys:

//Get Single Picture
var PictureResults = (from m in DB.Media where m.MediaID == 450 select m).SingleOrDefault();

//Get Gallery Pictures and Gallery Title
var GalleryResults = from g in DB.Galleries
                     join m in DB.Media on g.GalleryID equals m.GalleryID into gm
                     where g.GalleryID == 100
                     select new { g.GalleryTitle, Media = gm };

Here's my viewmodels.

public class GalleryViewModel
{
    public Media Media { get; set; }
    public IEnumerable<Gallery> Gallery { get; set; }
}

public class Gallery
{
    public string GalleryTitle { get; set; }
    public int MediaID { get; set; }
    public int GalleryID { get; set; }
    public string MediaGenre { get; set; }
    public string MediaTitle { get; set; }
    public string MediaDesc { get; set; }
}

The squigally line error occurs under GalleryResults:

//Create my viewmodel
var Model = new GalleryViewModel
{
    Media = PictureResults,
    Gallery = GalleryResults
};

解决方案

Ufuk Hacıoğulları has posted an answer and deleted it a few minutes later. I think his answer was correct and his solution gets rid of the error message. So I'm posting it again:

Ufuk Hacıoğulları's answer;

You are projecting a sequence of anonymous type instead of Gallery. Just instantiate Gallery objects in your select statement and it should work.

var GalleryResults = from g in DB.Galleries
                     join m in DB.Media on g.GalleryID equals m.GalleryID into gm
                     where g.GalleryID == 100
                     select new Gallery { GalleryTitle = g.GalleryTitle, Media = gm };

这篇关于无法隐式转换类型'System.Linq.IQueryable&LT; AnonymousType#1&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆