.NET Core / EF Core:由其他异步方法调用的主缓存查询

问题描述 投票:0回答:1

我有一个由多个查询共享的主子查询:

private List<MyCustomClass> Foo() 
{
    var t = _ctx.Table1
                .Where(...)
                .Select(s => new MyCustomClass { ... })
                .ToList();

    // IMemoryCache the result here 
 
    return t;
}

理想情况下,此方法可以是同步或异步的,是否有效并不重要。

我想在异步查询中使用它的结果:

public Task<List<OtherClass>> MyAmazingMethod() 
{
    return await Foo()
                    .Where(...)
                    .GroupBy(...)
                    .Select(s => new OtherClass { ... })
                    .ToListAsync();
}

可以做这样的事情吗?

非常感谢。

c# linq asp.net-core entity-framework-core
1个回答
0
投票

是的,这是可能的。 您可以将同步方法更新为此

public List<OtherClass> MyAmazingMethod()
{
    var fooResult = Foo();

    return fooResult
        .Where(...)
        .GroupBy(...)
        .Select(s => new OtherClass { ... })
        .ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.