Nothing big, just a small LINQ query [modified]
-
I've recently been working on a small application which tells me which user groups I'm in. After tossing WMI aside in this case due to slowness, I discovered the System.Security.Principal namespace:
IEnumerable<string> groups = from IdentityReference irc in currentPrinciple.Groups
let value = irc.Translate(typeof(NTAccount)).Value
let index = value.IndexOf('\\')
select value.Substring(index + 1);modified on Monday, May 18, 2009 3:49 AM
-
I've recently been working on a small application which tells me which user groups I'm in. After tossing WMI aside in this case due to slowness, I discovered the System.Security.Principal namespace:
IEnumerable<string> groups = from IdentityReference irc in currentPrinciple.Groups
let value = irc.Translate(typeof(NTAccount)).Value
let index = value.IndexOf('\\')
select value.Substring(index + 1);modified on Monday, May 18, 2009 3:49 AM
I've not really worked with .Net 3.5 and Linq but shouldn't the last line be
select arr[count - 1]
? -
I've not really worked with .Net 3.5 and Linq but shouldn't the last line be
select arr[count - 1]
? -
I've not really worked with .Net 3.5 and Linq but shouldn't the last line be
select arr[count - 1]
?Yeah, that seems right, just tried it myself, needs
-1
. -
Yeah, that seems right, just tried it myself, needs
-1
. -
I've recently been working on a small application which tells me which user groups I'm in. After tossing WMI aside in this case due to slowness, I discovered the System.Security.Principal namespace:
IEnumerable<string> groups = from IdentityReference irc in currentPrinciple.Groups
let value = irc.Translate(typeof(NTAccount)).Value
let index = value.IndexOf('\\')
select value.Substring(index + 1);modified on Monday, May 18, 2009 3:49 AM
I don't see why you feel a need to check for null here; have you experienced any nulls? Plus the Split is needless (and costly). I wrote it as:
public static System.Collections.Generic.IEnumerable
GetGroups1
(
System.Security.Principal.WindowsIdentity Identity
)
{
foreach
(
System.Security.Principal.IdentityReference grp
in
Identity.Groups
)
{
string temp = grp.Translate ( typeof(System.Security.Principal.NTAccount) ).Value ;
int offs = temp.IndexOf ( '\\' ) ;yield return ( offs == -1 ? temp : temp.Substring ( offs + 1 ) ) ; } yield break ;
}
Which appears to be about 3% quicker than yours. Here's yours without the Split:
public static System.Collections.Generic.IEnumerable
GetGroups3
(
System.Security.Principal.WindowsIdentity Identity
)
{
return
(
(from System.Security.Principal.IdentityReference irc in Identity.Groups
let value = irc.Translate(typeof(System.Security.Principal.NTAccount)).Value
let offs = value.IndexOf ( '\\' )
select offs == -1 ? value : value.Substring ( offs + 1 ) )
) ;
}It's about 2.6% quicker than yours.
-
I don't see why you feel a need to check for null here; have you experienced any nulls? Plus the Split is needless (and costly). I wrote it as:
public static System.Collections.Generic.IEnumerable
GetGroups1
(
System.Security.Principal.WindowsIdentity Identity
)
{
foreach
(
System.Security.Principal.IdentityReference grp
in
Identity.Groups
)
{
string temp = grp.Translate ( typeof(System.Security.Principal.NTAccount) ).Value ;
int offs = temp.IndexOf ( '\\' ) ;yield return ( offs == -1 ? temp : temp.Substring ( offs + 1 ) ) ; } yield break ;
}
Which appears to be about 3% quicker than yours. Here's yours without the Split:
public static System.Collections.Generic.IEnumerable
GetGroups3
(
System.Security.Principal.WindowsIdentity Identity
)
{
return
(
(from System.Security.Principal.IdentityReference irc in Identity.Groups
let value = irc.Translate(typeof(System.Security.Principal.NTAccount)).Value
let offs = value.IndexOf ( '\\' )
select offs == -1 ? value : value.Substring ( offs + 1 ) )
) ;
}It's about 2.6% quicker than yours.
I really hadn't thought about it like that. Thanks for the tips, and I've changed the snippets accordingly. FWIW, there may not be any need to use
offs == -1 ? value : value.Substring ( offs + 1 ) )
. Ifoffset
is -1, thenvalue.Substring(0)
will just returnvalue
, so there's no need for the conditional (unless you're counting string sizes in memory) As for the nulls, I did indeed experience some, but couldn't reproduce them. Having looked in Reflector, I can't find anything like that; I may have just imagined them -
I really hadn't thought about it like that. Thanks for the tips, and I've changed the snippets accordingly. FWIW, there may not be any need to use
offs == -1 ? value : value.Substring ( offs + 1 ) )
. Ifoffset
is -1, thenvalue.Substring(0)
will just returnvalue
, so there's no need for the conditional (unless you're counting string sizes in memory) As for the nulls, I did indeed experience some, but couldn't reproduce them. Having looked in Reflector, I can't find anything like that; I may have just imagined them:doh: Of course! That eliminates the offs variable.
yield return ( temp.Substring ( temp.IndexOf ( '\\' ) + 1 ) ) ;
-
:doh: Of course! That eliminates the offs variable.
yield return ( temp.Substring ( temp.IndexOf ( '\\' ) + 1 ) ) ;
-
Probably my coding style, but I would personally prefer to keep the offs variable. If I ever step through a piece of code, I want to be able to have every input where I can see it
I'll occasionally have to add such things to debug, but then I remove them again.