Yet another reason why I love LINQ-To-SQL
Nov. 7th, 2008 11:00 amYes, its another geeky entry. You'll just have to deal. :)
Anyways. So as anyone who has been reading this knows, I've been working with LINQ-To-SQL in the new project. This has been amazingly productive - things that previously took multiple lines of code and a conversion from the SQL layer to the business logic now takes one line.
For instance - an item on a survey, which may have one or more comments regarding it in a separate table, which we need to copy into a List so that we can serialize it and pass it to javascript using JSON.
The original approach using ADO.NET (but not DataSets, because I hate DataSets with a passion) would have required that I create a SqlConnection, then create a SqlCommand object paramaterized with a SurveyItemId and including a join to the User table so that I can get the user's name who left the last comment. I would then need to get a SqlDataReader from this and iterate through the results, creating a new comment object for each row, while being careful to cast each and every element in the SqlDataReader.
All in all, this would probably have been 10 - 15 lines of code. And some debugging since there is no compile-time checking for SQL statements.
The same code in LINQ:
Uh, yeah.
Anyways. So as anyone who has been reading this knows, I've been working with LINQ-To-SQL in the new project. This has been amazingly productive - things that previously took multiple lines of code and a conversion from the SQL layer to the business logic now takes one line.
For instance - an item on a survey, which may have one or more comments regarding it in a separate table, which we need to copy into a List so that we can serialize it and pass it to javascript using JSON.
The original approach using ADO.NET (but not DataSets, because I hate DataSets with a passion) would have required that I create a SqlConnection, then create a SqlCommand object paramaterized with a SurveyItemId and including a join to the User table so that I can get the user's name who left the last comment. I would then need to get a SqlDataReader from this and iterate through the results, creating a new comment object for each row, while being careful to cast each and every element in the SqlDataReader.
All in all, this would probably have been 10 - 15 lines of code. And some debugging since there is no compile-time checking for SQL statements.
The same code in LINQ:
_priorComments.AddRange(surveyItem.SurveyItemComments.Select(Function(sic) New SerializedComment(sic)))
Uh, yeah.