【问题标题】:OData filter for string array (Collection)字符串数组(集合)的 OData 过滤器
【发布时间】:2021-03-29 16:14:34
【问题描述】:

API 如下所示,我需要使用 postman 应用 Odata 过滤字符串数组。

需要做任何 C# 代码或过滤器就足够了吗?实际上 Category 是 String[]。

{ 
    "value": [
     {
            "Id": "10",
            "Name": "A1",
            "Category": ["a", "b"]
    },
    {
            "Id": "11",
            "Name": "A2",
            "Category": ["c", "b"]
    },
    {
            "Id": "13",
            "Name": "A3",
            "Category": ["d", "f"]
    }
]
}

需要过滤类别'b'并且输出应该

{ 
    "value": [
     {
            "Id": "10",
            "Name": "A1",
            "Category": ["a", "b"]
    },
    {
            "Id": "11",
            "Name": "A2",
            "Category": ["c", "b"]
    } 
]
}

能否请任何人帮忙处理 OData 过滤器?

【问题讨论】:

    标签: c# asp.net-core-webapi asp.net-core-3.1


    【解决方案1】:

    根据您的描述,因为 Category 是一个字符串数组。要过滤数据,您可以使用 any 运算符。请参考以下示例代码:

    1. 创建一个 CategoryInfo.cs:

       public class CategoryInfo
       {
           public int Id { get; set; }
           public string Name { get; set; }
           public string[] Category { get; set; }
       }
      
    2. Microsoft.AspNetCore.OData NuGet 包添加到项目中,并配置OData。

       public class Startup
       {
           public Startup(IConfiguration configuration)
           {
               Configuration = configuration;
           }
      
           public IConfiguration Configuration { get; }
      
           public void ConfigureServices(IServiceCollection services)
           {
               ...
               services.AddControllers(mvcOptions =>
                   mvcOptions.EnableEndpointRouting = false);
      
               services.AddOData(); 
           }
      
           public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
           { 
               ...
               app.UseRouting();
               app.UseMvc(routeBuilder =>
               {
                   routeBuilder.Select().Filter();
                   routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
                   routeBuilder.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
               }); 
           }
           IEdmModel GetEdmModel()
           {
               var odataBuilder = new ODataConventionModelBuilder();
               odataBuilder.EntitySet<CategoryInfo>("ToDo");
      
               return odataBuilder.GetEdmModel();
           }
       }
      
    3. 在 API 操作方法中添加 [EnableQuery()]

       [Route("api/[controller]")]
       [ApiController]
       public class ToDoController : ControllerBase
       {
           // GET: api/<ToDoController>
           [EnableQuery()]
           [HttpGet] 
           public IEnumerable<CategoryInfo> Get()
           {
               return new List<CategoryInfo>()
               {
                   new CategoryInfo(){ Id=10, Name = "A1", Category= new string[]{"a","b"}},
                   new CategoryInfo(){ Id=11, Name = "A2", Category= new string[]{"c","b"}},
                   new CategoryInfo(){ Id=12, Name = "A3", Category= new string[]{"d","f"}}
               };
           } 
      
    4. 使用 Postman 检查以下请求:

       https://localhost:44339/odata/ToDo?$filter=Category/any(d:contains(d, 'b'))
      

      这样的结果(请注意区分大小写):

    参考:

    Sample: Build web APIs with OData support using ASP.NET Core

    Experimenting with OData in ASP.NET Core 3.1

    Case Insensitive Search Filter in OData ASP.NET Core 2.0 Web API as an OWIN Middleware

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 2021-01-23
      • 2020-06-13
      • 2010-11-20
      • 2015-06-09
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2011-12-05
      相关资源
      最近更新 更多