我需要使用包含数据库数据的模型填充我的文章ViewModel,但我有一个方法需要分配给我的一个属性
图像列表是需要该方法的属性。 对于文章列表中的每个项,都会调用该方法一次。
这是我的代码:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var query = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName,
}).ToList();
articleViewModel.Images = imageService.GetImagesForArticle(articlemodel.Id.ToString());
return View(query);
}
我也尝试将方法放在linq中:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var query = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName,
Images = imageService.GetImagesForArticle(a.Id.ToString())
}).ToList();
return View(query);
}
它引发了一个例外: 类型&#39; System.NotSupportedException&#39;的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理
其他信息:LINQ to Entities无法识别方法&#39; System.Collections.Generic.List`1 [New_MinecraftNews_Webiste_MVC.Models.ImageInfo] GetImagesForArticle
答案 0 :(得分:0)
我在最后添加了一个foreach循环,但它确实有效:
public ActionResult ArticleTypes(string at)
{
articleViewModel.Images = new List<ImageInfo>();
var modelList = (from a in db.Articles
where a.SelectedArticleType == at
select new ArticlesViewModel
{
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName
}).ToList();
foreach (var model in modelList)
{
model.Images = imageService.GetImagesForArticle(model.Id.ToString());
}
return View(modelList);
}