简体   繁体   English

无法隐式转换类型'System.Linq.IQueryable <AnonymousType#1>'

[英]Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'

Im getting an error in my C# project which is causing me a headache. 我在C#项目中遇到错误,这让我很头疼。 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: 这是我的LINQ查询:

//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: 在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. UfukHacıoğulları已经发布了一个答案并在几分钟后删除了它。 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; UfukHacıoğulları的回答;

You are projecting a sequence of anonymous type instead of Gallery . 您正在投射一系列匿名类型而不是Gallery Just instantiate Gallery objects in your select statement and it should work. 只需在select语句中实例化Gallery对象即可。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable &lt;&gt;&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>' 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;to&#39;System.Linq.IQueryable <AnonymousType#2> “ - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Linq.IQueryable<AnonymousType#2>' 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable <MH_CRUD_Model.Member> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Linq.IQueryable<MH_CRUD_Model.Member>' 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#6> &#39;到&#39;十进制&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#6>' to 'decimal' 无法隐式转换System.Linq.IQueryable类型 <string> 串起来 - Cannot implicitly convert type System.Linq.IQueryable<string> to string 无法隐式转换类型&#39;System.Linq.IQueryable? - Cannot implicitly convert type 'System.Linq.IQueryable? MVC 5无法将类型&#39;System.Linq.IQueryable隐式转换为bool吗? - MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool? 无法将类型'System.Linq.IQueryable <int>'隐式转换为'int?' - Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?' 无法隐式转换类型&#39;System.Linq.IQueryable <double> &#39;到&#39;加倍&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<double>' to 'double' 无法隐式转换类型&#39;System.Linq.IQueryable - Cannot implicitly convert type 'System.Linq.IQueryable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM