2
目前,我有以下,從LDAP拉。如何按姓和名排序,然後按SamAccountName排序,哪裏並非所有名字都有第一個和最後一個?
// Get context, based on currently logged on user
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
// Search for the "Domain Users" group, with domain context
GroupPrincipal group = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Domain Users");
if(group != null) {
List<Principal> members = null;
members = group.GetMembers(false)
.OrderBy(principal => principal.Name).ToList();
List<Principal> Principals = members
.OrderBy(a => a.Name.Split(',')[0])
.ThenBy(a => a.Name.Split(',')[1])
.ThenBy(a => a.SamAccountName).ToList();
}
我遇到的問題是,如果我有李四,約翰它工作正常,但如果我有Admin01,它崩潰,因爲它不能找到第二部分。我該如何改變它,如果它只有一個單詞,它也會起作用?