You have several problems with your code:
- You ignore
id
passed: imagine that I call List(1234)
, 1234
will be ignored as well as 789
in List(789)
- You don't loop over
id
since you return after the 1st
loop
- You'll never reach
return View();
code: you enter loop and return from it.
I guess, you want something like this:
using System.Linq;
...
public IActionResult List(int id) {
// for given id we collect all corresponding FilialeVM items
var listafiliali = _repoFil
.GetById(id) // items correspond to given id
.Result
.Select(fil => _mapper.Map<FilialeVM>(fil)) // mapped to FilialeVM
.ToList(); // organized as list
// If we have any item in listafiliali, view them, otherwise use default View()
return listafiliali.Any()
? View(listafiliali)
: View();
}
Or if you don't want id
to be passed but to scan id = [0..1000]
range:
public IActionResult List() {
// for id in [0..1000] range we collect all corresponding FilialeVM items
var listafiliali = Enumerable
.Range(0, 1000 + 1)
.SelectMany(id => _repoFil
.GetById(id) // items correspond to given id
.Result
.Select(fil => _mapper.Map<FilialeVM>(fil))) // mapped to FilialeVM
.ToList(); // organized as list
// If we have any item in listafiliali, view them, otherwise use default View()
return listafiliali.Any()
? View(listafiliali)
: View();
}