Asp.net 核心 - Entity Framework 核心 - 向视图对象添加额外数据,同时保持其可查询

Asp.net core - Entity Framework core - add additional data to a view object while keeping it a Queryable

    [EnableQuery]
    public ActionResult<IQueryable<OurView>> GetInteractionRecordings(int pageNumber)
    {
        IQueryable<OurView> ourViews = _dbContext.OurView
            .Skip((pageNumber - 1) * 100)
            .Take(100)
            .AsQueryable();

        // One of the fields in OurView is a PhotoPath (string) that's not in the database.
        // We want to add that PhotoPath to every row without enumerating (function call for each row)
        // As we want to keep it a queryable

        return Ok(ourViews);
    }

无论采用何种解决方案,最重要的是我们要避免的是:

假设数据库 table 中有 500,000 条记录,在执行“Take”或 PhotoPath 查找之前在 table 中枚举所有这些记录。

我们希望在使用 Odata 时将其作为 Queryable。

您可以通过 Select:

var ourViews = _dbContext.OurView
    .Skip((pageNumber - 1) * 100)
    .Take(100);

var photoPath = "SomePath";

var withAdditionalData = ourViews
    .Select(x => new 
    {
        Id = x.Id,
        // other fields

        PhotoPath = photoPath
    });