【发布时间】:2020-09-10 18:17:17
【问题描述】:
我正在尝试在服务方法上手动创建 OData 查询。
如果可能,需要在 ODataQueryOptions 中创建变量。过滤器要求在示例中,Description = "Furniture"
http://localhost/odata/GetProductTypeDto()?$filter=Description eq 'Furniture'
代码:
public async Task<ProductTypeDto> GetProductTypeDto(ODataQueryOptions<ProductTypeDto> odataQueryOptions)
{
var dto = (IQueryable<ProductTypeDto>)odataQueryOptions.ApplyTo(_productRepository.GetProductTypes)();
var dtoList = await dto.ToListAsync();
return dto;
}
_productRepository.GetProductTypes() = // IQueryable which brings out this result {
new ProductTypeDto {ProductTypeId = 1, Description = "Furniture"},
new ProductTypeDto {ProductTypeId = 2, Description = "Electronics"},
new ProductTypeDto {ProductTypeId = 3, Description = "Books"}
}
我们现在尝试在变量 odataQueryOptions 中创建过滤器 Furniture,而不使用 API。
稍后尝试对某些内容进行单元测试,
var actualResult = product.GetProductTypeDto(odataQueryOptions)
Assert.Equals(actualResult.ProductTypeId, 1);
Assert.Equals(actualResult.Description, "Furniture");
【问题讨论】:
-
还不清楚,你是问如何以编程方式(无API)将带有过滤器的URL构造为字符串?或者您是在询问如何对 OData 端点进行单元测试?
标签: c# .net asp.net-core .net-core odata