目前,我有如下,从LDAP拉。
Currently, I have the below, pulling from LDAP.
// 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,它崩溃,因为它无法找到的第二部分。我如何改变它,如果它只有一个字,将工作呢?
The problem I run into is that if I have Doe,John it works fine, but if I have Admin01, it crashes because it can't find the second part. How do I change it so that if it is only one word it will work as well?
首先,我会把它投射到一个复合型,这样你就不会拆了个遍。 然后,我会用? :运营商。然后投影回得到原始类型。
First, I would project it to a compound type so that you don't split over and over. Then I would use a ? : operator. Then project back to get the original type.
members
.Select( m => new { member = m, split = m.Name.Split(',') } )
.OrderBy( a => a.split[0] )
.ThenBy( a => a.split.Count() > 1 ? a.split[1] : string.Empty )
.ThenBy( a => a.member.SamAccountNamr )
.Select( a => a.member );
下一篇:PNG文件验证文件、PNG