Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 基于示例从数据库上下文返回可枚举项_C#_Entity Framework_Asp.net Web Api2 - Fatal编程技术网

C# 基于示例从数据库上下文返回可枚举项

C# 基于示例从数据库上下文返回可枚举项,c#,entity-framework,asp.net-web-api2,C#,Entity Framework,Asp.net Web Api2,我开发了一个ASP.NET Web API 2.2,我希望数据来自我的dbcontext 到目前为止,我是这样做的: return _context.Client; [HttpGet("[action]")] public IEnumerable<Client> GetClients() { return Enumerable.Range(1, 5) .Select(index => new Client

我开发了一个ASP.NET Web API 2.2,我希望数据来自我的
dbcontext

到目前为止,我是这样做的:

return _context.Client;
[HttpGet("[action]")]
public IEnumerable<Client> GetClients()
{
    return Enumerable.Range(1, 5)
                     .Select(index => new Client
                                      {
                                          Name = "John",
                                          Surname = "Smiths"
                                      });
}
我希望以
IEnumerable
类型返回相同的数据,如下所示:

return _context.Client;
[HttpGet("[action]")]
public IEnumerable<Client> GetClients()
{
    return Enumerable.Range(1, 5)
                     .Select(index => new Client
                                      {
                                          Name = "John",
                                          Surname = "Smiths"
                                      });
}
[HttpGet(“[action]”)
公共IEnumerable GetClients()
{
返回可枚举范围(1,5)
.选择(索引=>新客户端
{
Name=“John”,
姓氏=“史密斯”
});
}

我如何才能做到这一点,让John和Smith被上下文中的连续数据所取代?

\u上下文。客户端已经是
IEnumerable
,可以按原样使用。例如:

[HttpGet("[action]")]
public IEnumerable<Client> GetClients(int fromId, int toId)
{
    return _context.Client.Where(c => c.Id <= toId && c.Id >= fromId).Take(100);
}
[HttpGet(“[action]”)
公共IEnumerable GetClient(int-fromId,int-toId)
{
返回_context.Client.Where(c=>c.Id=fromId).Take(100);
}

这只是一个示例,您可能需要执行不同的查询。

好的,有些内容已更改

我编辑了getclient方法:

[HttpGet("[action]")]
public  string GetClient()
{
    var settings = new JsonSerializerSettings()
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        Error = (sender, args) =>
        {
            args.ErrorContext.Handled = true;
        },
    };

    return JsonConvert.SerializeObject(_context.Client, settings);
}
现在,当我在vs中运行它时,我得到了一个包含正确数据的数组。在IIS上,我还得到一个数组,只是它是空的,但没有错误


我应该在Api中配置一些东西以便正确连接到DB吗?我在appsettings.json中有conn字符串,Startup addDbContext,还有什么吗?

你想用
范围做什么?
?好吧,没什么,我只想要数据库中的整个集合,这个范围只是VS2017中自定义angular/api2.2模板的示例。我需要它,否则angular会抛出一个关于解析JSON的错误。(我一周多以来一直在努力解决的问题,更多信息请参见我的个人资料)好的,那么,您可以从
\u上下文中返回客户机。。。要强制IEnumerable,您可以执行
.ToList()
问题;为什么特别要返回
IEnumerable
?作为HTTP帧,这并没有真正的意义,所以。。。为什么不让它成为
列表
T[]
,或者更明显的东西呢?好吧,我正在尽我所能,api返回的数据,我希望以后获取角度组件。问题是只有硬编码的数据才能工作,当我返回上下文时,angular应用程序中出现错误:在XMLHttpRequest.lMy点的JSON.parse()处JSON输入的意外结束是,只要类型继承自
IEnumerable
(必须继承该类型才能编译代码),那么
AsEnumerable
调用就没有意义。在这种情况下不需要它。很少需要它-主要是指示从
IQueryable
到“LINQ到对象”的转换(即,将LINQ执行从数据库转移到内存中)。请注意,
IQueryable
(即
context.Client
是)是
IEnumerable
,并且代码是编译的,您不应该将IQueryables泄漏到DAL之外(在返回它之前将其转换为
ToList()
)。泄漏IQueryables违反了您的层分离,并将导致编译器无法捕获的错误和意外行为。