[英]C# Asp.net Membership.GetAllUsers order by email
I am using Membership.GetAllUsers() to get my user list. 我正在使用Membership.GetAllUsers()来获取我的用户列表。 I would like to have the list returned sorted by email address as I need to flag some accounts with duplicate emails.
我希望按电子邮件地址排序返回列表,因为我需要标记一些带有重复电子邮件的帐户。
Membership.GetAllUsers() seems to order by username by default. Membership.GetAllUsers()似乎默认按用户名排序。 Is there a way to change this behavior?
有没有办法改变这种行为?
If you can live with a generic list rather than a MembershipUserCollection
: 如果您可以使用通用列表而不是
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. 如果您想要不区分大小写的电子邮件地址
OrderBy(x => x.Email, StringComparer.OrdinalIgnoreCase)
请使用OrderBy(x => x.Email, StringComparer.OrdinalIgnoreCase)
。
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. 成员代码实际上早于泛型(它从.NET 1.1开发集成到.NET 2.0中),因此
MembershipUserCollection
不实现通用接口。
Given your earlier question , you may be interested in other LINQ-style manipulations. 鉴于您之前的问题 ,您可能对其他LINQ风格的操作感兴趣。 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: 例如,下面将为您提供一个字典,其键是电子邮件地址(不区分大小写),其值是相应的
MembershipUser
对象的列表,按上次活动日期降序排序:
var dictionary = Membership.GetAllUsers().Cast<MembershipUser>()
.GroupBy(x => x.Email, StringComparer.OrdinalIgnoreCase)
.ToDictionary(
x => x.Key,
x =>x.OrderByDescending(u => u.LastActivityDate).ToList()
);
The following code should do it, but it may not have the best performance. 以下代码应该这样做,但它可能没有最佳性能。
Membership.GetAllUsers().Cast<MembershipUser>().OrderBy(m => m.Email);
It does the sorting in memory, rather than as a db query. 它在内存中进行排序,而不是作为db查询。
Membership.GetAllUsers().OrderBy(user => user.Email).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.