entity-framework-core - 如何在 Entity Framework Core 中过滤结果集?
问题描述
我有一个用 Entity Framework Core 编写的非常小的 Web 服务,它返回已经、正在或将要退火的线圈列表。我不在乎那些已经退火的。如何将过滤器过滤掉完成的过滤器?
该Inventory
表包括一列archived
。我想查询archived
列包含零的那些行。我怎么做?
查询正在IEndpointRouteBuilder
接口上的扩展方法中执行:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Text.Json;
using CAPSWebServer.CapsDataModels;
namespace Microsoft.AspNetCore.Builder
{
public static class CAPSServiceEndpoint
{
public static void MapWebService(this IEndpointRouteBuilder app)
{
app.MapGet("caps/coils", async context =>
{
CapsDataContext data = context.RequestServices.GetService<CapsDataContext>();
var coils = data.Inventories;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonSerializer.Serialize<IEnumerable<Inventory>>(data.Inventories));
});
}
}
}
解决方案
假设archived
是一个布尔值,你应该能够做到:
await data.Inventories.Where(inventory => !inventory.Archived).ToListAsync();
.
你很可能需要一个using
forSystem.Linq
和Microsoft.EntityFrameworkCore
推荐阅读
- mongodb - MongoDB 中的 Spring Boot 动态更新
- ios - 用于回复 App Store 评论的 API?
- r - 曲线下的填充区域 - R 图
- python - 我在使用 gunicorn 部署项目时犯了一个错误
- ruby-on-rails - 在 Ruby 中安装 I18n 后多次失败
- ruby-on-rails - 如何在 JSON 中公开继承列
- r - while (abs(G.dev.old - G.dev) > c.crit && iter < n.cyc) 中的 gamlss 错误
- javascript - 引导多选:默认值和 onChange 不起作用
- git - 使用 ssh-agent 调用 docker-compose
- javascript - 未捕获(承诺中)类型错误:无法读取未定义的属性“绑定”