B
Billy_1991
Guest
I'm fetching products from a database and preparing them as DTO's which will be returned to frontend:
public async Task<IEnumerable<ProductDTO>> Get(Request request)
{
IQueryable<Product> query = _context.Products;
var data = query.ToList();
var products = await query.ToListAsync();
// WARNING FOR EACH ROW NEW DB CALL?
return Map(products).Select(c =>
{
c.HasChildren = data.Any(cc => cc.ParentProductId == c.Id);
return c;
});
}
private IEnumerable<ProductDTO> Map(IEnumerable<Product> products)
{
return products.Select(p => MapData(p)).ToList();
}
Now I'm suspicious with this code here where I'm checking if my product has some children products:
return Map(products).Select(c =>
{
c.HasChildren = data.Any(cc => cc.ParentProductId == c.Id);
return c;
});
My friend said that this code mean that for each item in my `products` list I will make database request to check if there are any items with this condition :
cc => cc.ParentProductId == c.Id
And I thought it's not true because I'm querying memory list??
If I'm making new request for each item than this will be really bad for performance so how this might be solved?
Thanks
CHEERS
Continue reading...
public async Task<IEnumerable<ProductDTO>> Get(Request request)
{
IQueryable<Product> query = _context.Products;
var data = query.ToList();
var products = await query.ToListAsync();
// WARNING FOR EACH ROW NEW DB CALL?
return Map(products).Select(c =>
{
c.HasChildren = data.Any(cc => cc.ParentProductId == c.Id);
return c;
});
}
private IEnumerable<ProductDTO> Map(IEnumerable<Product> products)
{
return products.Select(p => MapData(p)).ToList();
}
Now I'm suspicious with this code here where I'm checking if my product has some children products:
return Map(products).Select(c =>
{
c.HasChildren = data.Any(cc => cc.ParentProductId == c.Id);
return c;
});
My friend said that this code mean that for each item in my `products` list I will make database request to check if there are any items with this condition :
cc => cc.ParentProductId == c.Id
And I thought it's not true because I'm querying memory list??
If I'm making new request for each item than this will be really bad for performance so how this might be solved?
Thanks
CHEERS
Continue reading...