I have the following LINQ query in my repository which behaves as expected:
public ICollection<Address> GetManufacterAddressesFromProductId(Guid productId)
{
return Context.Products
.First(product => product.Id == productId)
.Manufacturer
.Addresses;
}
How do I make this call async? This is my current solution, which seems... overly long?
public async Task<ICollection<Address>> GetManufacterAddressesFromProductIdAsync(Guid productId)
{
var product = await Context.Products.FirstAsync(x => x.Id == productId);
var manufacturerId = product.ManufacturerId;
return await Context.ManufacturerAddresses.Where(x => x.ManufacturerId == manufacturerId ).ToListAsync();
}
Leaves me feeling like this isn't really doing things properly, as it has to wait for the first call to complete anyway - sort of defeats the point?
CodePudding user response:
In the following way:
public Task<ICollection<Address>> GetManufacterAddressesFromProductId(Guid productId)
{
return Context.Products
.Where(product => product.Id == productId)
.Select(product => product.Manufacturer.Addresses)
.FirstAsync();
}
Also note that Sync version is not effective. Should be:
public ICollection<Address> GetManufacterAddressesFromProductId(Guid productId)
{
return Context.Products
.Where(product => product.Id == productId)
.Select(product => product.Manufacturer.Addresses)
.First();
}