Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Nothing big, just a small LINQ query [modified]

Nothing big, just a small LINQ query [modified]

Scheduled Pinned Locked Moved Clever Code
databasecsharplinqsecurity
10 Posts 3 Posters 29 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0x3c0
    wrote on last edited by
    #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

    R P 2 Replies Last reply
    0
    • 0 0x3c0

      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

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      I've not really worked with .Net 3.5 and Linq but shouldn't the last line be select arr[count - 1]?

      0 P 2 Replies Last reply
      0
      • R Robert Rohde

        I've not really worked with .Net 3.5 and Linq but shouldn't the last line be select arr[count - 1]?

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        No, the format of the output is ComputerName\UserGroup. If you're sharing across a network it'd be useful, but it looks messy IMHO Edit: My mistake. I refactored it to add null-checking and made a mistake

        modified on Sunday, May 17, 2009 11:55 AM

        1 Reply Last reply
        0
        • R Robert Rohde

          I've not really worked with .Net 3.5 and Linq but shouldn't the last line be select arr[count - 1]?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Yeah, that seems right, just tried it myself, needs -1.

          0 1 Reply Last reply
          0
          • P PIEBALDconsult

            Yeah, that seems right, just tried it myself, needs -1.

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            After a quick check, that's right. I've just updated the snippet

            1 Reply Last reply
            0
            • 0 0x3c0

              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

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              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.

              0 1 Reply Last reply
              0
              • P PIEBALDconsult

                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.

                0 Offline
                0 Offline
                0x3c0
                wrote on last edited by
                #7

                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 ) ). If offset is -1, then value.Substring(0) will just return value, 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

                P 1 Reply Last reply
                0
                • 0 0x3c0

                  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 ) ). If offset is -1, then value.Substring(0) will just return value, 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

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  :doh: Of course! That eliminates the offs variable. yield return ( temp.Substring ( temp.IndexOf ( '\\' ) + 1 ) ) ;

                  0 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    :doh: Of course! That eliminates the offs variable. yield return ( temp.Substring ( temp.IndexOf ( '\\' ) + 1 ) ) ;

                    0 Offline
                    0 Offline
                    0x3c0
                    wrote on last edited by
                    #9

                    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

                    P 1 Reply Last reply
                    0
                    • 0 0x3c0

                      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

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      I'll occasionally have to add such things to debug, but then I remove them again.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups