I am having problem in performing OrderBy on a sub-property of an Object.
I have two separate Models as listed below:
PersonModel:
public class PersonModel
{
public string Name { get; set;}
public DeptModel Dept { get; set; }
}
DeptModel
public class DeptModel
{
public int DeptId { get; set;}
public string DeptName { get; set;}
}
I had used automapper to map the relation between those two class and I am able to retrieve data properly.
After the data had been retrieved, I am trying to implement order by using linq on the DeptName object. Then it is showing an error "Object reference not set to an instance of an object."
The result is retrieving data as IList < PersonModel >. If Debug over here it is showing the correct data at this point.
Then I am performing the OrderBy on the retrieved data and it is crashing over here.
var tmp = result.OrderBy(x => x.Dept.DeptName).ToList();
I am not sure why I getting this error. Any suggestions on resolving the issue is greatly appreciated.