gpt4 book ai didi

c# - 转换 IQueryable 以实现 IAsyncEnumerable

转载 作者:太空狗 更新时间:2023-10-29 20:04:15 25 4
gpt4 key购买 nike

<分区>

我在一个方法中有一个查询:

private readonly IEntityReader<Customer> _reader;

public async Task<IEnumerable<Customer>> HandleAsync(GetCustomer query)
{
var result = _reader.Query()
.Include(customer => customer.Organization)
.Where(customer => customer.Name == query.Name);

return await result.ToListAsync();
}

其中有这个单元测试:

[Fact]
public async Task HandleGetCustomer_ReturnsCustomer_WhenNameMatches()
{
// Arrange
var customers = new List<Customer>()
{
new Customer
{
Id = new Guid("d4e749ba-6874-40f4-9134-6c9cc1bc95fe"),
Name = "John Doe",
Age = 18,
Organization = new Organization
{
Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
Name = "TheCompany",
Number = 42
}
},
new Customer
{
Id = new Guid("0679ceb5-3d4f-41f3-a1b0-b167e1ac6d7e"),
Name = "Another Guy",
Age = 39,
Organization = new Organization
{
Id = new Guid("b2ba06c9-5c00-4634-b6f7-80167ea8c3f1"),
Name = "TheCompany",
Number = 42
}
}
}.AsQueryable();

var entityReader = new Mock<IEntityReader<Customer>>(MockBehavior.Strict);
entityReader.Setup(reader => reader.Query()).Returns(customers);

var query = new GetCustomer
{
Name = "John Doe"
};

var handler = new HandleGetCustomer(entityReader.Object);

// Act
var result = await handler.HandleAsync(query);

// Assert
Assert.NotNull(result);
Assert.True(result.Count() == 1);
Assert.True(result.FirstOrDefault().Id == new Guid("d4e749ba-6874-40f4-9134-6c9cc1bc95fe"));
}

显然调用 .ToListAsync(); 失败是因为:

The source IQueryable doesn't implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.

扩展方法是来自 Entity Framework Core 的方法。

如何将我的客户列表转换为 IAsyncEnumerable

作为引用,IEntityReader 接口(interface)的定义如下:

public interface IEntityReader<out TEntity> where TEntity : Entity
{
IQueryable<TEntity> Query();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com