I'm new to MVC and databases, but I'm fairly certain the way that I'm getting data from the database is incredibly inefficient.
public class PokemonViewController : Controller
{
private PokemonDayCareDBEntities1 db = new PokemonDayCareDBEntities1();
[Authorize]
public ActionResult Pokemon(int id)
{
var pkmn = db.PlayerPkmns.ToList();
PlayerPkmn thispkmn = null;
//get pokemon from database where id = id
foreach (var item in pkmn)
{
if (item.Id == id)
{
thispkmn = item;
}
}
....
I have the unique ID of the Pokemon that resides in the PlayerPkmns table, but I'm looping through the entire table in order to find the matching ID.
Depending on how large the table, the time this takes to execute would increase.
I'm positive there's a better way - would anyone know if there is one (also, the syntax on how to use it)
Thanks!
db.PlayerPkmns.Where(x=>x.Id == id).FirstOrDefault();FirstOrDefaulttakes a predicate.whereI mean behind the scenes...Whereis superfluous