I'm building an API application and when I call this service method the way it's below, it just works fine:
public IEnumerable<Produtos> GetAll(string term, int page, int pageSize)
{
var products = _repository.GetAllProducts().Skip((page - 1) * pageSize).Take(pageSize).ToList();
return products;
}
It returns a List of objects. Fair enough.
But then, when I implement this logic, it fails to retrieve me any data:
public IEnumerable<Produtos> GetAll(string term, int page, int pageSize)
{
var products = _repository.GetAllProducts().Skip((page - 1) * pageSize).Take(pageSize).ToList();
List<Produtos> produtos = new List<Produtos>();
products.ForEach(produto =>
{
Console.WriteLine(produto);
if (produto.Nome.Contains(term))
{
Console.WriteLine("if statement");
produtos.Add(produto);
}
}
);
if (!(produtos.Count > 0))
{
throw new Exception();
}
return produtos;
}
Also, the IDE tells me that there is not an instantiated object:
Strangely, when I debug it, it stops looping over the list when it reaches product.name == "kn"
Here's my db with random values I did insert into.