Your error message suggests that either a t
is null
or a t.ParentCategory
is null
.
You can fix the error by simply checking for null
s, but this may or may not give you the desired output, depending on whether you also want to include categories that don't have a parent.
model.GroupedTypeOptions = this.UnitOfWork.CategoryRepository.Get()
.Where(t => t.ParentCategory != null)
.OrderBy(t => t.ParentCategory.Name).ThenBy(t => t.Name)
.Select(t => new GroupedSelectListItem
{
GroupKey = t.ParentId.ToString(),
GroupName = t.ParentCategory.Name,
Text = t.Name,
Value = t.Id.ToString()
});
I'm assuming your CategoryRepository
can't return a null
t
, but if it can, you'd adapt the where to be:
.Where(t => t != null && t.ParentCategory != null)