MVC - 指定的强制转换无效
本文关键字:转换 无效 MVC | 更新日期: 2023-09-27 18:31:08
MVC的新手,检查了很多解决方法..没有帮助:
这段代码写在模型仓库中,它返回一个列表,我可以返回到 View(Index)
IList<AccessArticles> publisherList = (from aa in db.AccessArticles
join u in db.Users on aa.UserId equals u.id
join a in db.Articles on aa.ArticleId equals a.id
where u.id == id
orderby a.title
select new
{
UserName = u.username,
ArticleTitle = a.title,
AccessArticlesId = aa.AccessArticlesId,
ArticleId = aa.ArticleId,
UserId = aa.UserId
})
.AsEnumerable()
.Select
(x=> new AccessArticles
{
AccessArticlesId = x.AccessArticlesId,
UserId = x.UserId,
UserName = x.UserName,
ArticleTitle = x.ArticleTitle
}).ToList();
我收到的错误是指定的强制转换无效。
请建议一个出路或更好的方法来获得相同的效果。
谢谢
代码参考 -
接口:
List<AccessArticles> SearchByUserId(int UserId);
存储 库:
public List<AccessArticles> SearchByUserId(int id)
{
var setArticles = (from aa in db.AccessArticles
join u in db.Users on aa.UserId equals u.id
join a in db.Articles on aa.ArticleId equals a.id
where u.id == id
orderby a.title
select new AccessArticles
{
UserName= u.username,
ArticleTitle = a.title,
AccessArticlesId= aa.AccessArticlesId,
ArticleId = aa.ArticleId,
UserId = aa.UserId
}).ToList();
return setArticles;
}
控制器
public ActionResult Index(int? id)
{
int UserId = 0;
if (id != null)
{
UserId = Convert.ToInt32(id);
}
IAccessArticlesRepository AccessRepository = new AccessArticlesRepository();
//int count = AccessRepository.SearchByUserId(UserId).Count();
List_AccessArticles = AccessRepository.SearchByUserId(UserId);
return View(List_AccessArticles);
}
型
namespace theSiteCMS.Models
{
public partial class AccessArticles
{
public string ArticleTitle { get; set; }
public string UserName { get; set; }
}
}
I did some changes taking some reference:
----------
Repository
----------
public IQueryable<AccessArticles> SearchByUserId(int id)
{
var setArticles = (from aa in db.AccessArticles
join u in db.Users on aa.UserId equals u.id
join a in db.Articles on aa.ArticleId equals a.id
where u.id == id
orderby a.title
select new AccessArticles
{
UserName= u.username,
ArticleTitle = a.title,
AccessArticlesId= aa.AccessArticlesId,
ArticleId = aa.ArticleId,
UserId = aa.UserId
});
return setArticles;
}
--------------
Controller
--------------
public ActionResult Index(int? id)
{
int UserId = 0;
if (id != null)
{
UserId = Convert.ToInt32(id);
}
IAccessArticlesRepository AccessRepository = new AccessArticlesRepository();
var userwisearticles = AccessRepository.SearchByUserId(UserId);
return View(userwisearticles);
}
Error:
In Index.aspx page:
<% foreach (var item in Model) { %>
Highlight on 'Model' - Explicit construction of entity type 'theSiteCMS.Models.AccessArticles' in query is not allowed.
这将返回具有下面描述的属性的匿名类型的集合。 如果需要使用现有类,请确保它不是 EF 用来表示实体的类,除非它直接匹配。 如果是这种情况,则应该能够将下面的select new
替换为select aa
,并让它返回 EF 为你生成的实体模型的列表。
var publisherList = (from aa in db.AccessArticles
join u in db.Users on aa.UserId equals u.Id
join a in db.Articles on aa.ArticleId equals a.Id
where u.Id == userId
orderby a.Title
select new
{
UserName = u.UserName,
ArticleTitle = a.Title,
AccessArticleId = aa.AccessArticlesId,
UserId = aa.UserId
}).ToList();
编辑以直接选择 EF 实体;
var publisherList = (from aa in db.AccessArticles
join u in db.Users on aa.UserId equals u.Id
join a in db.Articles on aa.ArticleId equals a.Id
where u.Id == userId
orderby a.Title
select aa).ToList();
这应该会触发编译错误,因为返回类型不匹配。 我猜它一定是List<AccessArticle>