I have the following model
public class UserViewModel
{
public String CVR_Nummer { get; set; }
public DateTime LastActivityDate { get; set; }
public DateTime CreationDate { get; set; }
public String FirmaNavn { get; set; }
public int ProcentAnswered { get; set; }
}
I create a List<UserViewModel> and try to sort it:
userviewmodel.OrderBy(x => x.ProcentAnswered);
It compiles, but the list doesnt get sorted. Howcome?
LINQ is side-effect free by design so it won’t change the input. Also it uses lazy execution, it won’t do anything till you try to reach the data. Re-assign the output to the list and it should work. Notice that I’m using ToList() method here because OrderBy() returns
IEnumerable<UserViewModel>and it’s not evaluated till we try to get the items in it. We create a new list from this sequence using ToList() method and forcing the query to execute.