Posts

Showing posts with the label LINQ

LINQ THINK

The more I use LINQ, the more I run into legacy code that can easily be improved. The nice thing about LINQ is that it can make your code more elegant and easier to understand, as well as, in most cases, more efficient. Let’s take a simple  example. Here I’ve extracted some legacy code in the form of a method that tells you if a given string “Key” is present in a string array. First, the “OLD” way: //"OLD" way: static bool IsKeyInArray(string[] items, string key) { if (items == null) return false; if (items.Length < 1) return false; if (String.IsNullOrEmpty(key)) return false; foreach (string item in items) { if (item.Trim() == key) { return true; } } return false; } The obvious  thing to see here is that we have to iterate over the entire array. Now the new “LINQY” way: static bool IsKeyInArray2(string[] items, string key) { bool isInArray = false; if (items == null || items.Length <1 || String.IsNullOrEmpty( key)) return false; if (items.ToList().Contains(key...

LINQ To SQL / Entity Framework / NHibernate ORM Top-Down, Objects First

People demand freedom of speech as a compensation for the freedom of thought which they seldom use . – Soren Kirkegaard In the process of stumbling through LINQ To SQL to see if I would be able to represent a SQL Server database schema I created to provide storage for a hierarchical well-defined XML Schema for a commonly used utility object, I came to the realization that I was indeed doing everything completely backwards ! What I am saying is this:  ORM should be done by focusing on the OBJECTS FIRST, not the Database Schema! Unfortunately, most of the tools we have are data-centric, not object-centric.  Scott Allen has a post that clearly describes the debacle . To my knowledge, there will not be any plain old CLR objects (POCOs) in Entity Framework. LINQ to SQL doesn’t yet have all the mapping capability to really separate the object model from the underlying database schema – and of course, you can use it with SQL Server only. Now, this situation may imp...

Entity Framework Goodness

Image
I finally got around to the Entity Framework BETA 2, and I like it already. To play with this, you need these bits; Entity Framework Beta 2: http://www.microsoft.com/downloads/details.aspx?FamilyID=f1adc5d1-a42e-40a6-a68c-a42ee11186f7&displaylang=en Tools, Aug 2007 CTP: http://www.microsoft.com/downloads/details.aspx?FamilyId=09A36081-5ED1-4648-B995-6239D0B77CB5&displaylang=en There is also a nice "Getting Started" piece on codeplex: Getting Started: http://www.codeplex.com/adonetsamples/Release/ProjectReleases.aspx?ReleaseId=7792 I decided to brave it and just roll a quickie on my own from the Northwind database. All you do is Add New Item / ADO.NET Entity Data Model, which gives you a wizard to choose what database objects you want modeled, and then you get a nice class diagram: All your entity model code is built for you. So to say, get 10 products from the Products table via a nice little LINQ query: // Get 10 products via Entity LINQ query using (NorthwindMod...