简体   繁体   English

ASP.net 核心:将过滤列表上的 OData 属性填充为 IQueryable

[英]ASP.net Core: Populating OData properties on the filtered list as IQueryable

I have a fully working OData API which is returning the right responses for all of my domain objects, which is great.我有一个完全可用的 OData API,它为我的所有域对象返回正确的响应,这很棒。 The part I'm struggling with is how to populate objects with values that are not entities.我正在努力解决的部分是如何使用非实体的值填充对象。

I have a list of InventoryItem s which has a list ReadingTags but when I try to iterate through the ReadingTags after converting to a list of DTOs it seems that I have access to the entire list of InventoryItem s as the execution of the data has been deferred until I send the serialize the response to the user.我有一个InventoryItem列表,其中有一个ReadingTags列表,但是当我在转换为 DTO 列表后尝试遍历ReadingTags时,似乎我可以访问整个InventoryItem列表,因为数据的执行已被推迟直到我将响应序列化发送给用户。 The response from a third-party web service will then be merged into the DTO before being sent as the response.来自第三方 web 服务的响应将在作为响应发送之前合并到 DTO 中。

What is the best way to do this?做这个的最好方式是什么? I've looked at middleware, and filters and I'm at a loss.我看过中间件和过滤器,但不知所措。 For reference, here is my code:作为参考,这是我的代码:

Domain entities:领域实体:

public class InventoryItem
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ISet<ReadingTag> ReadingTags { get; set; } = new HashSet<ReadingTag>();
}

public class ReadingTag
{
    public virtual Guid Id { get; set; }
    public virtual string DeviceName { get; set; }
    // Can't add this to the domain as it's not a database/entity property
    //public IList<double> Readings { get; set; } = new List<double>();
}

DTO entities: DTO实体:

public class InventoryItemDto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public IList<double> Readings { get; set; } = new List<double>();
}

Controller: Controller:

[HttpGet]
[EnableQuery]
[ProducesResponseType(200)]
public ActionResult Get()
{
    var query = applicationService.All();
    var results = _mapper.Map<List<InventoryItemDto>>(query); // Automapper
    return Ok(results);
}

Service:服务:

public Task<IQueryable<InventoryItem>> All()
{
    var inventoryItems = _session.Query<InventoryItem>();
    // I want to be able to send the filtered response to another service to populate the Readings of the ReadingTags
    return inventoryItems;
}

It's fixed, I can now filter within the service and work with the DTO too, thank you for your help:它已修复,我现在可以在服务中进行过滤并使用 DTO,感谢您的帮助:

Install-Package AutoMapper.AspNetCore.OData.EFCore
[HttpGet]
[ProducesResponseType(200)]
public IActionResult Get(ODataQueryOptions<InventoryItem> options)
    {
    var results = _readingService.All(options);
    return Ok(results);
}
public IList<InventoryItemDto> All(ODataQueryOptions<InventoryItem> options)
{
    var query = _session.Query<InventoryItem>();
    var inventoryItems = query.GetQuery(_mapper, options); // Filter applied here!
    var results = _mapper.Map<List<InventoryItemDto>>(inventoryItems);
    // Removed for brevity
    return results;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM