-
Donn Felker recently blogged about a neat little extension method in LINQ called Any() . If you simply want to know if a sequence contains any elements, many people use ".Count() > 0" which will walk the entire sequence to compute the count whereas .Any() will stop walking as soon as it finds a single element. Easy and much more efficient. It reminded me about another LINQ method I've used from time to time: ToDictionary(). This method will allow you to quickly create a dictionary from any IEnumerable<T>. Let's start with some sample data that is in a List<T>: IList<Person> people = new List<Person> { new Person {FirstName = "Bob" , LastName = "Smith" , SSN = "1" }, new Person {FirstName = "Jane" , LastName = "Doe" , SSN = "2" }, new Person {FirstName = "Mike" , LastName = "Johnson" , SSN = "3" } }; Converting this to a dictionary is pretty trivial without LINQ. Suppose we want to index these by SSN: var d = new Dictionary< string , Person>(); foreach (var p in people) { d.Add(p.SSN, p); } But why waste our time doing all that when we can simply use LINQ's ToDictionary()? Just give it a lambda that selects the key and you're all set: var indexedBySSN = people.ToDictionary(k => k.SSN); Or perhaps you don't want the entire Person object. Maybe you just want a collection of last names indexed by Social Security Number (SSN). No problem --there's an overload that accepts two lambdas: one to select the key and one to select the value. var lastNamesIndexedBySSN = people.ToDictionary(k => k.SSN, e => e.LastName); There's also two more overloads that work the same as the two above, but allow you to also provide an IEqualityComparer<T> used to compare your keys. Everyday I find more and more stuff in LINQ that helps me eliminate the mundane code and make my source more readable. Technorati Tags: .NET , LINQ
-
Holiday week? Lots to do before fireworks on the weekend. Tomorrow night is Ignite Ann Arbor. If you haven't attending Ignite talks before, check them out! If you have, I'm sure that you'll be there. Format is 5 minutes per speaker, 20 slides...
-
Eric Gunnerson has a post in which he responds to Justin Etheredge regarding “Is Programming A Generic Skill?”
I’ll just say that I agree with them both. There are differences between ability, proficiency and mastery.
A Java programmer can jump into C# and be an able C# programmer immediately. It will take time to become proficient and [...]
-
After using Python , diving into jQuery has proven exceptionally difficult. Even though it solves the not-so-small problem of commonizing the DOM across multiple browsers, I find it very difficult to read. I know with time I’ll get used to it and maybe...
-
Once again, we brought together a small group to discuss two more upcoming topics for the software industry. This time it was Natural User Interfaces and Social Media. The most interesting observation is that there is a bigger gap between the future-oriented...
-
As part of Jon Udell's Interviews with Innovators series, he talks to Joan Peckham about Computational Thinking for Everyone. She is a involved in an NSF project that's evaluating computational thinking in the context of K-12 education. I love...
-
Last Tuesday, we hosted our first executive briefing on upcoming technology trends. The first two topics were Cloud Computing and Rich Internet Applications (RIA). We chose those topics because examples of RIA applications are already around, and...
-
After a year or so of hosting public lightning talks on Fridays, we're finding that it's no longer necessary for us to continue to do so. We're happy to say that other groups have introduced lightning talks and are holding them in the evenings...
-
F.cs(334,8): error CS0539: ‘I.M’ in explicit interface declaration is not a member of interface F.cs(20,15): error CS0535: ‘C’ does not implement interface member ‘I.M(out string)’ This is a fun example of a poor error message, and I don’t mean because I named my file F, my interface I, my method M [...]
-
I've been doing some more TeeChart work for a client, and ran into the following two issues. This is actually the second time I have gone through this pain, so I'm writing them down this time in the hopes that they become imprinted on my brain...
-
A few months ago, Bill Wagner (my business partner at SRT Solutions) and I were talking about how companies were really pulling together in this economy and doing what they could to help others. I was really impressed with the brillant marketing of Atlassian...
-
A friend was complaining today that he didn't get Facebook. I mentioned that I didn't either. That's not actually true. I do get it; it just serves a different purpose than twitter. Or LinkedIn. So I thought that I would write a bit about...
-
The number one feature that has been in Opera for longer than I can remember, and in Firefox via add-in or by default for nearly as long, is that of restoring a session when the browser or system crashes.
I lean on this feature. I use browser tabs as a todo list. Sometimes I have to-read [...]
-
Yesterday, we hosted a Software Development Jam at Automation Alley in Troy, MI (North suburbs of Detroit, MI for those readers not in the Michigan area). We billed the event as a way for developers to learn something about those software techniques that...
-
A couple posts ago I was doing some experimentation with TCPClient and TCPServer. The bulk of my testing was done by creating a simple chat application. After that I decided I wanted to figure out how to do the same thing using WCF. The goal was to have...