Published Friday, May 18, 2007 2:16 AM by mtaulty

Thinking in LINQ

For the first time, I'm writing bits of code that are nothing to do with LINQ but I'm using the V3.0 C# compiler and so I'm starting to think "Hey, I could use a bit of LINQ" here.

Just a moment ago, I wanted to find any network interface that had an available IPV6 address. It took me a little while to write;

 

     UnicastIPAddressInformation ipAddressInfo =
            (
              from ni in NetworkInterface.GetAllNetworkInterfaces()
              from ad in ni.GetIPProperties().UnicastAddresses
              where ni.OperationalStatus == OperationalStatus.Up
                && ad.Address.AddressFamily == AddressFamily.InterNetworkV6 
              select ad
            ).First();

Now, I'm not sure at this point that it actually works - Mike's first Law of Software states;

Single Step The Code in a Debugger whilst Applying a Mindset of "How Many Places Can I Make This Fail?".

so there's something in there about LINQ in that, in these early days, it's easy to formulate a query that's neat and highly readable (and so maintainable) but it's also not so easy (for me) to be confident yet that I've actually written the right thing. Maybe that'll get easier over time after I've single-stepped a few of these things and seen the right results pop out.

# Link Listing - May 18, 2007 @ Friday, May 18, 2007 8:05 PM

WCF Addressing In-Depth [Via: Aaron Skonnard ] Demo code for Mix07 talk: Silverlight: Creating and Delivering...

Christopher Steen