Enumerator failed to MoveNextAsync 处理思路

之前的写法

public async Task<myMap> GetMapAsync(
            string CompanyId, string specs)
        {
            ICollection<myMap> result = _fcpDbContext.myMaps
                .Where(item => item.CompanyId== CompanyId)
                .AsNoTracking()
                .ToList();

            if (...)
            {
                return result.FirstOrDefault();
            }
            else {
                if (result.Count == 1)
                {
                    return result.FirstOrDefault();
                }
                else { 
                    return await _fcpDbContext
                        .myMaps
                         .SingleOrDefaultAsync(item => item.CompanyId== CompanyId
                         && item.Specification.Contains(specs.Contains("停车费")?"停车费": specs));
                    
                }
            }
        }

问题分析:

return await _fcpDbContext
            .myMaps
             .SingleOrDefaultAsync(item => item.CompanyId== CompanyId
             && item.Specification.Contains(specs.Contains("停车费")?"停车费": specs));
                    

查询里嵌套了判断,可能引发问题

更改1:

public async Task<myMap> GetMapAsync(
            string CompanyId, string specs)
        {
            ICollection<myMap> result = _fcpDbContext.myMaps
                .Where(item => item.CompanyId== CompanyId)
                .AsNoTracking()
                .ToList();

            if (...)
            {
                return result.FirstOrDefault();
            }
            else {
                if (result.Count == 1)
                {
                    return result.FirstOrDefault();
                }
                else { 
                	string specstext = specs.Contains("停车费") ? "停车费" : specs;
                    return await _fcpDbContext
                        .myMaps
                         .SingleOrDefaultAsync(item => item.CompanyId== CompanyId
                         && item.Specification.Contains(specstext));
                    
                }
            }
        }

依旧报错

按照debug,依旧是最后一句出的问题

改最后一句写法:

更改2:

public async Task<myMap> GetMapAsync(
            string CompanyId, string specs)
        {
            ICollection<myMap> result = _fcpDbContext.myMaps
                .Where(item => item.CompanyId== CompanyId)
                .AsNoTracking()
                .ToList();

            if (...)
            {
                return result.FirstOrDefault();
            }
            else {
                if (result.Count == 1)
                {
                    return result.FirstOrDefault();
                }
                else { 
                	string specstext = specs.Contains("停车费") ? "停车费" : specs;
                    return result.Where(item => item.Specification.Contains(specstext)).FirstOrDefault();
                    
                }
            }
        }

问题解决

问题原因后面再找找,
可能和这个有关?
If the enumerator isn’t in a good state for MoveNextAsync to be called (for example, it’s already been disposed), then it just returns the equivalent of “new ValueTask(false).” Otherwise, it resets the ManualResetValueTaskSourceCore for the next iteration and calls (via a runtime helper) the MoveNext method just shown.

如果有帮助你的话,能点个赞吗?

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值