LINQ Questions, Some Answers

In comments, Mark Mehelis asked some good questions re. LINQ:

  • As far as I can tell the syntax between VB and C# is not the same. Do you know why this is? Would it not be better to have this be consistent accross the languages?

The syntax does indeed vary between languages. A consistent query language would definitely have been easier to learn, but what LINQ provides is a set of consistent query operators that can be incorporated into languages in whatever way the language designer sees fit. This makes LINQ slightly less teachable (it will vary from language to language) but perhaps more appealing to language designers, since they aren’t forced to accept a query language that differs from their own language design philosophy. Vick (VB) is emphasizing familiarity and a SQL-like syntax, Hejlsberg (C#) is emphasizing functional-style programming forms. This is also a good place to point out that languages can use the underlying APIs even if they do not implement language extensions.

  • Is there some way to have all your LINQ based access through a repository of some type. It would be to easy to pepper queries throughout your code making it a string pulling excersize when your application gained some size.

This is an interesting idea. I haven’t worked with actual LINQ code enough to actually wrap my head around how it affects coupling and cohesion, so I don’t know. We don’t yet know what patterns are productive in LINQ-using programs. Obviously, you could put all your queries in a single class behind a façade and even put that class into its own namespace / assembly. Would that be a good idea? Not sure.

  • Also what happens when the underlying data structure in the DB changes? Must you re-compile your code? recreate your classes and then change your LINQ queries?

You definitely should not have to recompile on a DB change, you almost certainly do if you’re querying objects.