之前的写法
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.
如果有帮助你的话,能点个赞吗?