I am getting the following error when trying to return a list of new objects from a linq query. I am looking to return a stripped down entity for use in a selectbox and only need and id and name.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
myViewModel.Regions = _regionRepository
.OrderBy(x => x.Name)
.Select(x => new RegionForSelect {Id = x.Id.ToString(), Name = x.Name})
.ToList();
public class MyViewModel
{
public IList<RegionForSelect> Regions { get; set; }
}
public class RegionForSelect
{
public string Id;
public string Name;
}
Not sure where I am going wrong here.
Any tips appreciated.