Dot Net Tips and Tricks

Custom Search

Archive for May, 2007

C# 3.0 LINQ Wow!

I just read this months article in the MSDN magazine (C# Evolution) and I’m excited about this addition to C-Sharp in the next version. I have worked as a DBA and find adding the from, where, select, orderby, group by, let, and join to objects in .NET most interesting. Alot of times, I’ve used techniques like this to accomplish the goals but the verbage becomes lengthy. C# 3.0 solves this problem.

LINQ has the following parts:

  • Lambda Expressions are a language feature that is similar in many ways to anonymous methods. The basic idea is that you can treat code as data.
    • StaticMethods.Where(customers, c => c.ZipCode == 91822);
  • Extension methods are basically static methods that are callable through an instance syntax.
    • The above Where method would be an Extension Method. It would be exposed on an instance member though. customers.
    • public static IEnumerable<T> Where<T>(this IEnumerable<T> items, Func<T, bool> predicate)
  • Anonymous Types and Implicitly Typed Local Variables can be used together to specify types “On the fly” inside a method.
    • Instead of create a placeholder class (Customer name and address) for each time you want to pull something out of a collection, you declare a var variable and use an anonymous type to populate it.
    • val custAddress = new { FullName = c.FirstName , HomeAddress = c.Address };
  • Object initializers basically allow the assignment of multiple properties or fields in a single expression.
    • Create an instance of a class without overloading the constructor a million times.
    • Customer customer = new Customer() { Name = bob, Address = Wilco Way };
  • These all lead up to Query Expressions. SQL like ways of accessing .NET objects.
    • var locals = from c in customers

      where c.ZipCode == 91822

      select new { FullName = c.FirstName, HomeAddress = c.Address };

Coming from both a coding and dba background, I find these enhancements in “Orcas” wonderful. It could save a ton of time in coding.

Do yourself a favor and go checkout C# 3.0 and “Orcas”.

No comments

VisualSVN and Windows Workflow Problem

After installing the V1.2 upgrade, I’ve found that any solution with Windows Workflow used will not open and will crash Visual Studio 2005. I’ve submitted a request to their site and will post back more when I have it.

05/09/2007 –>A bug fix release has been release: V1.2.1 that now works with Windows Workflow projects.

No comments