我收到此错误
Cannot implicitly convert type
System.Collections.Generic.List<AnonymousType#1>
toSystem.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; }
}
View 模型
public class ViewTeamList
{
public List<TeamDetails> TeamNext { get; set; }
}
Controller
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....;
最佳答案
这会构造一个匿名类型而不是 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>
关于c# - 无法将类型 'System.Collections.Generic.List<AnonymousType#1>' 隐式转换为 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540818/