I’m having trouble sorting a list of user profiles which I am passing to the view. I want to display the list of all users in a certain role and I want to sort them by familyName attribute.
I tried using OrderBy but it has no effect.
Code in the controller
public ActionResult Index()
{
//get all patients
var patients = Roles.GetUsersInRole("user").ToList();
//set up list of patient profiles
List<UserProfile> pprofiles = new List<UserProfile>();
foreach (var i in patients) {
pprofiles.Add(ZodiacPRO.Models.UserProfile.GetUserProfile(i));
}
pprofiles.OrderBy(x => x.familyName); //<-this has no effect the list produced is
// exactly the same it was without this line
return View(pprofiles);
}
And the View
<ul id= "patientList">
@foreach (var m in Model)
{
<li>
<ul class="patient">
<li class="ptitle">@m.title</li>
<li class="pname"> @Html.ActionLink(@m.givenName + " " + @m.familyName, "View", "Account", new { @username = @m.UserName.ToString() }, new { id = "try" })</li>
<li class="pprofile">@Ajax.ActionLink("Profile", "PatientSummary", new { @username = @m.UserName }, new AjaxOptions { UpdateTargetId = "pContent"},new{ @class = "profpic" })</li>
</ul>
</li>
}
</ul>
I will need to reuse this in more than one place and there could be a large number of users so not ordering them in someway would be terrible. How should I go about this?
pprofiles.OrderBy(x => x.familyName);will return anIEnumerable<T>, not sorting the array where it was called on.You can change your code like this :
Or in a more Linq-styled way :
Or :