If you create a new class library in VS2010 using .NET 4, the default target framework is a new package called .NET 4 Client Profile which is a subset of the full .NET 4 (more info at http://msdn.microsoft.com/en-us/library/cc656912.aspx). The Client Profile is useful for for streamlining client application installations, but it causes some odd behavior in Visual Studio 2010 with regards to referenced assemblies.
Basically, if you reference another assembly that makes use of references outside the .NET 4 Client Profile, you’ll get full intellisense before you build. However, your build will fail. And afterwards, intellisense will disappear for those unsupported assemblies and the IDE will mark them with squiggly lines indicating that they’re unknown classes. At first it seems as though the dll was deleted or something, but it wasn’t. It’s just that the default target framework for Class Libraries in Visual Studio 2010 is the .NET 4 Client Profile which doesn’t support something you’re doing. I assume that the reason for the weird behavior has something to do with the build-as-you-type functionality of VS2010 not paying full attention to this.
Anyway, the solution to this is just to go to your project settings (right click on project and select properties) and under the Application tab (top tab) like so:

I’ve been conducting interviews over the last couple weeks and have noticed a general lack of understanding regarding Lambda expressions (even in those candidates with senior role titles). So here’s some information about Lamdas that if you don’t already know, you should!
First of all, Lambda expressions are just inline methods. Unlike delegates, lambdas have access to variables that are scoped the same as the lambda. So if you have a method with a variable v and a lambda l, the statements inside l will have access to v as in the following example:
1: void SomeMethod(){
2: int v = 1;
3: Func<int> l = () => { return v + 1 };
4: ...
5: }
simple enough, right? Except, and here’s what I believe separates the good from the great, do you know how that works? Think about it; in that example, l is a method and it doesn’t take v as a parameter, so v should be out of its scope. Like it would be with a delegate. But we already said it’s not. And the reason has to do with a cute compiler trick which pulls out the necessary variables and tacks them onto their own class. This is called variable lifting. The lambda expression is also tacked onto this class, so it has access to these variables (which otherwise would be out of scope).
The reason the how-it-works matters is all in the side effects. Now that you know how it works you should be able to figure out what the output would be from the following snippet:
1: int i = 1;
2: Func<int> f = () => { return i; };
3: i = 10;
4: Console.WriteLine(f());
Did you get it? The output is 10. Because by the time the Func is called, i was set to 10. Since i is lifted into its own class, the same i is changed a line after the initialization of f. So by the time we execute f, i is 10.
Consider the difference this makes for the following code:
1: string[] strings = {"this is a sentence", "here's another one", "this should be plenty of sentences", "we're probably done here" };
2:
3: var query = strings.AsQueryable();
4: foreach(var s in new string[] {"s", "p"})
5: {
6: query = query.Where(x => x.Contains(s));
7: }
8: foreach (var item in query)
9: Console.WriteLine(item);
Most developers I’ve spoken to assume that the query will have 2 predicates (where contains “s” and contains “p”) so the output will be:
this should be plenty of sentences
but in fact, the output is:
this should be plenty of sentences
we’re probably done here
because the query that’s built will basically look like:
1: query.Where(x => x.Contains(“p”)).Where(x => x.Contains(“p”))
since the s variable will be lifted and will change through each iteration. By the time you get around executing the query, s will be set to the last string in the array. The way around this is to create a placeholder variable inside the foreach loop like this:
1: ...
2: foreach(var s in new string[] {"s", "p"})
3: {
4: var localString = s;
5: query = query.Where(x => x.Contains(localString));
6: }
7: ...