无法从'System.Generic.Collection.List'进行转换

问题描述 投票:0回答:1

我不想返回对象列表,但得到:

无法从'System.Generic.Collection.List<ErrorDashboard_DatabaseEF.parsde_errors>'转换为'ErrorDashboard_DatabaseEF.parsed_errors'

使用FirstOrDefault()时我只能返回一个对象,但是我需要在指定日期之间的每个对象。

控制器

public ActionResult GetAllLogsWithMatchingHash(string hash)
    {
        using (var repository = new repositoryCollection())
        {
           var result =  repository.ErrorLogsRepository.GetAllLogsByHash("dc03cd92c3fa7f9a647adb9dbf95ab54");
        }
        return Redirect("home");
    }

存储库

   public List<parsed_errorsModel> GetAllLogsByHash(string hashToSearchBy)
    {
        using (var context = new EdDbContext())
        {
            var dateFromToCheck = DateTime.Now.AddDays(-31);
            hashToSearchBy = "969da6814fa518a8fac3a03cc32378fc";
            var result =  context.parsed_errors.OrderByDescending(x=>x.TimeUtc).Where(c => c.TimeUtc > dateFromToCheck && c.HashDetails == hashToSearchBy).ToList();
            return parsed_errorTranslator.TranslateToModel(result); <-- here it where I get the error
        }
    }

我的翻译

 internal static parsed_errorsModel TranslateToModel (parsed_errors entity)
    {
        var result =  new parsed_errorsModel()
        {
            ErrorId = entity.ErrorId,
            Application = entity.Application,
            Host = entity.Host,
            Type = entity.Type,
            Source = entity.Source,
            Message = entity.Message,
            User = entity.User,
            StatusCode = entity.StatusCode,
            TimeUtc = entity.TimeUtc,
            Sequence = entity.Sequence,
            AllXml = entity.AllXml,
            Details = entity.Details,
            HashDetails = entity.HashDetails,
            PropertyPath = entity.PropertyPath,
            InternalErrorId = entity.InternalErrorId,
            RequestType = entity.RequestType,


        };
        return result;

    }

来自同一存储库的此请求有效

   public parsed_errorsModel GetErrorByTicketId(Guid ticketId)
    {
        using (var context = new EdDbContext())
        {
            var errorToGet = context.parsed_errors.FirstOrDefault(x => x.ErrorId.Equals(ticketId));
            return parsed_errorTranslator.TranslateToModel(errorToGet);
        }
    }

我感谢所有帮助。

c# .net model-view-controller
1个回答
3
投票

此行代码有两个问题:

© www.soinside.com 2019 - 2024. All rights reserved.