无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'到"System.Collections.Generic.List< FirstApp.Model.TeamDetails>" [英] Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

查看:62
本文介绍了无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>'到"System.Collections.Generic.List< FirstApp.Model.TeamDetails>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误

无法隐式转换类型 System.Collections.Generic.List<AnonymousType#1>System.Collections.Generic.List<FirstApp.Model.TeamDetails>

Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to 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; }
}

ViewModel

ViewModel

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....; 

推荐答案

这将构造一个匿名类型,而不是TeamDetails

This constructs an anonymous type instead of 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>

这篇关于无法隐式转换类型'System.Collections.Generic.List&lt; AnonymousType#1&gt;'到"System.Collections.Generic.List&lt; FirstApp.Model.TeamDetails&gt;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆