![](/img/trans.png)
[英]Cannot implicitly convert type 'System.Data.Linq.ISingleResult<""> to 'System.Collections.Generic.IEnumerable<" ">. An explicit conversion exists?
[英]Cannot implicitly convert type 'project_name.Models.model_name' to 'System.Collections.IEnumerable'. An explicit conversion exists
我只想打印帶有(ShowInDatabase == true)
的文章,但我遇到了:
RuntimeBinderException:無法將類型“PROIECT.Models.Article”隱式轉換為“System.Collections.IEnumerable”。 Views->Articles->Index 中存在顯式轉換(您是否缺少演員表?):@foreach(ViewBag.Articles 中的 PROIECT.Models.Article 文章)
在模型->文章中有這個
public bool ShowInDatabase { get; set; }
查詢:
var articles = db.Articles.Include("Domain").Include("User")
.Where(art=> art.ShowInDatabase == true)
.FirstOrDefault();
重要提示:如果我像這樣修改查詢,它會起作用:
var articles = db.Articles.Include("Domain").Include("User");
通過發送查看
ViewBag.Articles = articles;
在視圖->文章->索引
@foreach (project_name.Models.Article article in ViewBag.Articles)
單個對象和對象集合是兩個不同的事物。 換句話說……一個蘋果不是一籃子蘋果。
在這里,您正在獲取一個對象的單個實例:
var articles = db.Articles.Include("Domain").Include("User")
.Where(art=> art.ShowInDatabase == true)
.FirstOrDefault();
你不能遍歷它,因為它不是一個集合。 這是一個單一的對象。
所以你基本上有兩個選擇:
var articles = db.Articles.Include("Domain").Include("User")
.Where(art=> art.ShowInDatabase == true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.