Linq, writing sql in C# ?

27.aug I attended MSDN Live in Oslo. Although mainly a java and open source developer, I’ve worked with ASP.NET 2.0. I used Visual Studio 2005 and web parts, so I do have, if not a leg, at least a few toes in the .Net world as well.

There are some very good things coming with .Net 3.5 and C# 3.0. And the Linq is amongst the best. The introduction of SQL-like syntax in the OO-language first made me sick, but really it’s quite ingeniously the more I think about it. My objective as a programmer is to write easily read, maintainable, and testable code. Now I’m ashamed of my first reaction. Ashamed.

Try writing the following code object-oriented!

IEnumerable<MyClass> result =
from myClass in collection1
where property1 > criteria1
orderby property2 descending
select myClass

Probably looks messy if you are unfamiliar with writing with such a syntax. But if you reflect on the alternative, you’d have to use collections, comparators, ifs, loops and temporary containers. This is beautiful!

Linq is also a persistence API, which looks great as well, but that’s beyond what I wanted to emphasize here.

Scott Guthrie has written a lot about Linq to Sql. Read his blog for in deep examples and exploit the possibilities!

About Ole Morten Amundsen

Developer, programmer, entrepreneur. Java, .Net, ruby, rails, agile, lean. Opinionated enthusiast!
This entry was posted in .Net. Bookmark the permalink.

One Response to Linq, writing sql in C# ?

  1. follesoe says:

    Hi Ole Morten,

    Glad you liked the presentation, and that you managed to look past the “what the hell is SQL syntaxt doing in my C# code” feeling! And congrats with the new blog :-)

    Best regards,

    Jonas Follesø
    http://jonas.follesoe.no

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s