This is a strange problem I’m having, I have the following statement:
IEnumerable<CategoryListViewModel> categoryViewModels = categoryRepository.GetAllCategoryViewModels();
This correctly populates with exactly one element.
categoryViewModels.Where(cvm => cvm.CategoryId == localId).Single().Selected = true;
Yet, nothing gets updated, selected remains false. I can verify that the CategoryId and the localId are equal, if I break out:
var something = categoryViewModels.Where(cvm => cvm.CategoryId == localId).Single()
It correctly returns the one viewModel. In fact, if I go in and I set the local variable “something” to Selected = false. It’ll correctly update the local variable.
Am I missing something obvious?
Edit:
public class CategoryListViewModel
{
public Guid CategoryId { get; set; }
public string CategoryName { get; set; }
public bool Selected { get; set; }
}
I think your problem might be that
categoryViewModelsis actually anIQueryable– so when you enumerate it, then it makes a database call and SELECTs a list from the database.This means you create a list, and then change
selectedfor one item in that list.However… next time you enumerate
categoryViewModelsthen this causes another SELECT to occur – and a new list to be fetched into memory.To fix this, you could try
ToList():or you could reorganise your code in some other way to ensure only one underlying query is performed.