Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法用于通过任何API调用查询时的返回类型错误_C#_.net_.net Core - Fatal编程技术网

C# 无法用于通过任何API调用查询时的返回类型错误

C# 无法用于通过任何API调用查询时的返回类型错误,c#,.net,.net-core,C#,.net,.net Core,我遇到了一个错误,调用API时我似乎无法回避。错误是: System.ArgumentException: Expression of type 'System.Threading.Tasks.Task`1[System.Linq.IQueryable`1[System.String]]' cannot be used for return type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable

我遇到了一个错误,调用API时我似乎无法回避。错误是:

    System.ArgumentException: Expression of type 'System.Threading.Tasks.Task`1[System.Linq.IQueryable`1[System.String]]' cannot be used for return type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[System.String]]'
   at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters, String paramName)
   at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
   at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, Boolean tailCall, IEnumerable`1 parameters)
   at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, ParameterExpression[] parameters)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
当使用组织id提交API调用时,它应该抓取与该id关联的所有位置。这就是引发此错误的时间。我一辈子都找不到是什么原因造成的。电话详情如下:

控制器:

[HttpPost("{id}/getorganizationlocations")]
    public async Task<IActionResult> GetOrganizationLoc(int id)
    {
        var organizationNames = await _repo.GetOrganizationNameById(id);

        return Ok(organizationNames);
    }

你知道是什么导致了这一切吗

首先我想道歉,因为你是对的。你的问题是正确的

第二,我创建了一个应用程序,发现了问题:

这是EF 3.0.0中的一个已知错误

我可以通过管理Nuget软件包并将3个软件包升级到ER Core 3.1.3的最新版本来解决这个问题

升级这3个软件包后,一切正常


您的查询不是问题。

正如Jonathan Alfaro提到的,答案是将存储库中的FirstOrDefaultAsync更改为ToListSync。这确实导致抛出另一个转换错误,但是:

Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<string>>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)
通过将.Select更改为.SelectMany解决了此问题。工作版本如下:

        public async Task<IEnumerable<string>> GetOrganizationNameById(int LocationId)
{
    var OrganizationName = await _context.Locations
                .Include(l => l.Owners)
                .Where(o => o.Id == LocationId)
                .SelectMany(l => l.Owners.Select(on => on.Owner.OrganizationName))
                .ToListAsync();

    return OrganizationName;
}
公共异步任务GetOrganizationNameById(int-LocationId) { var OrganizationName=await\u context.Locations .包括(l=>l.所有者) .其中(o=>o.Id==LocationId) .SelectMany(l=>l.Owner.Select(on=>on.Owner.OrganizationName)) .ToListAsync(); 返回组织名称; }
dotnet core 3.0.101版和EF core 3.0.0版。我在谷歌上搜索了这个问题是的,但是没有关于这个问题的任何消息。有一些类似的问题提到它与不是一个异步方法有关,但在我的项目中它是异步的。很抱歉,FirstOrDefault意味着一个实体,这正是您得到的错误。。。错误表明您有IQueryable和not IQueryable。。。。。因此,是的,当它需要一个字符串集合时,您将返回一个字符串。因此,基本上您有两个选项:更改公共异步任务GetOrganizationNameById(int LocationId)。。。。。。。。。。。若要返回任务,请将FirstOrDefault更改为ToListThank。将其更改为ToListSync有效,但引发了转换错误。我最终需要更改。选择到。选择存储库中的许多。这使它工作,它没有返回正确的数据。再次感谢您在这个问题上向正确的方向推进。我认为如果您升级到3.1.3,您的查询可以工作。他们检查了我的答案,我发现这是一个已知的错误。升级到EF 3.1.3后,我的测试就结束了。啊,很高兴知道!说到这些,我是一个初学者(我通过使用.NETCore作为API和Angular作为前端构建自己的web应用程序来学习)。我很高兴看到这里还有另一个潜在的问题。谢谢你的跟进!
var organizationNames = await _repo.GetOrganizationNameById(id);
Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<string>>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)
        public async Task<IEnumerable<string>> GetOrganizationNameById(int LocationId)
{
    var OrganizationName = await _context.Locations
                .Include(l => l.Owners)
                .Where(o => o.Id == LocationId)
                .SelectMany(l => l.Owners.Select(on => on.Owner.OrganizationName))
                .ToListAsync();

    return OrganizationName;
}