我收到此错误
无法隐式转换类型
System.Collections.Generic.List<AnonymousType#1>
来System.Collections.Generic.List<FirstApp.Model.TeamDetails>
我的代码出了什么问题?
这是我的代码
TeamDetails
班级
public class TeamDetails
{
[Key]
public int TeamId { get; set; }
public string TeamName { get; set; }
public string Description { get; set; }
public int? UserCount { get; set; }
}
视图模型
public class ViewTeamList
{
public List<TeamDetails> TeamNext { get; set; }
}
控制器
public ActionResult Next(int dataid)
{
ViewTeamList viewTeamList = new ViewTeamList();
var a = from t in tDbContext.Teams
join u in tDbContext.Users on t.TeamId equals u.TeamId into g
where t.Deleted != true
select new { TeamId= t.TeamId,TeamName = t.TeamName, Description = t.Description, UserId = g.Count() };
var next = a.OrderBy(t1 => t1.TeamId).Where(t1 => t1.TeamId > dataid).FirstOrDefault();
viewTeamList.TeamNext = a.ToList();
return PartialView("_ViewTeamDetails", viewTeamList);
}
我无法将此值分配给
viewTeamList.TeamNext = a....;
答案 0 :(得分:5)
这构造了一个匿名类型而不是TeamDetails
select new { TeamId= t.TeamId,TeamName = t.TeamName, Description = t.Description, UserId = g.Count() }
您应该将其更改为
select new TeamDetails { TeamId = t.TeamId, TeamName = t.TeamName, Description = t.Description, UserCount = g.Count() }
所以a.ToList()
将是List<FirstApp.Model.TeamDetails>