linq 无效强制转换异常:无法将"系统.集合.泛型.列表“类型的对象强制转换为”系统.集合.泛型.IEnumerable

lbsnaicq  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(158)

我正在尝试在netcore中实现干净的架构,但遇到运行时错误InvalidCastException:无法将"系统.集合.泛型.列表"类型的对象强制转换为"系统.集合.泛型. IEnumerable
在WebUI中,我有Match控制器和ViewAllMatch动作,如下所示

public async Task<IActionResult> ViewAllMatch()
        {
            var matches = await _mediator.Send(new GetMatchesDetail());
            return View(matches);
        }

在应用层,我有一个如下的查询:

public class GetMatchesDetail : IRequest<IEnumerable<MatchesDetail>>
    {
    }
    public class MatchesDetail
    {
        public string MatchId { get; set; }
        public int MatchNumer { get; set; }
        public DateTime DateMatch { get; set; }
        public TimeSpan TimeMatch { get; set; }
        public int MatchYear { get; set; }
        public string SeasonId { get; set; }
        public string Round { get; set; }
        /// <summary>
        /// Set value to Qualified for Qualified and Final for Final Round
        /// </summary>
        public string Stage { get; set; }
        public string SubStage { get; set; }
        public string HTeam { get; set; }
        public string HTeamCode { get; set; } //For Flag get from Table Team from Foreign Key TeamName
        public int HGoal { get; set; }
        public int GGoal { get; set; }
        public string GTeam { get; set; }
        public string GTeamCode { get; set; }
        public string WinNote { get; set; }
        public string Stadium { get; set; }
        public string Referee { get; set; }
        public long Visistors { get; set; }
        public string Status { get; set; }
    }

    public class GetMatchesHandler : IRequestHandler<GetMatchesDetail, IEnumerable<MatchesDetail>>
    {
        private readonly IMatchRepository _matchRepository;
       
        public GetMatchesHandler(IMatchRepository matchRepository)
        {
            _matchRepository = matchRepository;
        }

        public async Task<IEnumerable<MatchesDetail>> Handle(GetMatchesDetail request, CancellationToken cancellationToken)
        {
            var matchlistview = await _matchRepository.GetMatchDetailAsync();

            return matchlistview;
        }
    }

matchRepository获取基础设施中所有匹配项的代码,如下所示。

public async Task<IEnumerable<MatchesDetail>> GetMatchDetailAsync()
        {
            var matchDetailList = (from match in _context.Matches
                                   join team1 in _context.Teams on match.HTeam equals team1.TeamName
                                   join team2 in _context.Teams on match.GTeam equals team2.TeamName
                                   join season in _context.Seasons on match.SeasonId equals season.SeasonId
                                   select new
                                   {
                                       match.MatchId,
                                       match.MatchNumber,
                                       match.DateMatch,
                                       match.TimeMatch,
                                       match.MatchYear,
                                       match.SeasonId,
                                       season.SeasonName,
                                       match.Round,
                                       match.Stage,
                                       match.SubStage,
                                       match.HTeam,
                                       HTeamCode = team1.TeamCode,
                                       match.HGoal,
                                       match.GGoal,
                                       match.GTeam,
                                       GTeamCode = team2.TeamCode,
                                       match.WinNote,
                                       match.Stadium,
                                       match.Referee,
                                       match.Visistors

                                   });

            return (IEnumerable<MatchesDetail>)await matchDetailList.ToListAsync();
        }

完整的代码已经上传到Github的https://github.com/nguyentuananh921/Betting.git.了解更多细节。
谢谢你的帮助。当我有更多的实体和我想在WebUI中查看的模型包含域中的许多实体时,我对清洁建筑技术中的模型感到困惑。

kgsdhlau

kgsdhlau1#

谢谢你的帮助。
我有这样的修改公共IEnumerable的GetMatchDetailAsync()。

public IEnumerable<MatchesDetail> GetMatchDetailAsync()
    {          

        #region TryOther way
        var matchQuery = (from match in _context.Matches
                               join team1 in _context.Teams on match.HTeam equals team1.TeamName
                               join team2 in _context.Teams on match.GTeam equals team2.TeamName
                               join season in _context.Seasons on match.SeasonId equals season.SeasonId
                               select new
                               {
                                   #region selectResult
                                   //Remove to clear Select what I want to get.
                                   #endregion

                               });
        MatchesDetail matchesDetail = new MatchesDetail();
        List<MatchesDetail> retList = new List<MatchesDetail>();
        //IEnumerable<MatchesDetail> retList;
        foreach (var item in matchQuery)
        {
            #region ManualMapping
                matchesDetail.MatchId = item.MatchId;
                //other field mapping

            #endregion
            retList.Add(matchesDetail);
        }
        #endregion
        return retList;

    }

而且很管用

相关问题