If you can live with a generic list rather than a MembershipUserCollection
:
Membership.GetAllUsers().Cast<MembershipUser>().OrderBy(x => x.Email).ToList();
Use OrderBy(x => x.Email, StringComparer.OrdinalIgnoreCase)
if you want a case-insensitive sort of the email address.
Membership code actually predates generics (it was integrated into .NET 2.0 from a .NET 1.1 development), hence MembershipUserCollection
does not implement generic interfaces.
Given your earlier question, you may be interested in other LINQ-style manipulations. For example the following will give you a dictionary whose keys are email addresses (case-insensitive), and whose values are lists of corresponding MembershipUser
objects, sorted by last activity date descending:
var dictionary = Membership.GetAllUsers().Cast<MembershipUser>()
.GroupBy(x => x.Email, StringComparer.OrdinalIgnoreCase)
.ToDictionary(
x => x.Key,
x =>x.OrderByDescending(u => u.LastActivityDate).ToList()
);