Home > .net 3.5, c#, LINQ, Oracle, Visual Studio 2008 > A Linq to Orcas

A Linq to Orcas

May 7, 2007

Okay, okay, so it’s early in the morning and I’m not funny.  I get that. Back on 1 May, I wrote about creating some helper functions for using delgates to Find/FindAll on collection objects.  Well, that’s fine and dandy for the .NET 2.0 framework, but I decided to kick the library that I’ve been dinking with up to 3.5.  My goal?  Remove some of the excess code and, when we move forward (rollout for this library isn’t planned until the fall—plenty of time for me to break things), have a framework that matches current .NET standards. On the first, I discussed that I could simplify:

StudentRoster studentsInGrade9 = 
    new StudentRoster(roster.FindAll( 
    delegate(StudentRecord x) { return x.Grade.Equals(“09”); }));

Down to:

StudentCollection studentsInGrade9 = 
   new StudentCollection(collection.FindAll(StudentCollection.GetByGrade(“09”)));

By adding static method to the StudentCollection object.

public static Predicate<StudentRecord> GetByGrade(string grade) 
{ 
     return (delegate(StudentRecord x) { return x.Grade.Equals(grade); }); 
}

Dandy. But… that’s kinda annoying and it requires there to be a “GetBy” method for EVERY parameter that I might want to search by.  That gets messy and requires maintenance.  Why not, if we’re using Framework 3.5, we use LINQ? Now, I’ll admit, this is my first venture into LINQ.  Laugh, I can take it, but this is pretty cool to me.  Also, a preface, many will wonder why I’m not using DLINQ and instancianting my database as an object using Metal or the little “I’m a DataSet, just cooler” designer tool.  Two painful words: Oracle database.  Until I can find a spiffy way to get either the designer or Metal to hit an Oracle database, I appear to be sunk.  If there is an Oracle “HOWTO” out there, I’d REALLY appreciate a postup. Now, on to the fun. Similar to the example above, if we want to search for all students in the ninth (“09”) grade, we can assign our results to the new var interface.  What is ‘var’?  It’s a strongly typed “container” that morphs into whatever you first pass it when you initialize it. Thus, if I type:

var int = 1;

var is of type Int32.  If I try to pass it a string value (after it’s initialized), I’ll recieve an error “Cannot implicitly convert type ‘string’ to ‘int’”. The same goes for passing objects—it allows us to create a container that will accept whatever is passed to it and then strongly type it—avoiding boxing and unboxing that occurs with objects.  Cool beans.

var ninthGraders = from s in students 
    where s.Grade == "09" 
    select s;   

foreach (var s in ninthGraders) 
{ 
    Console.WriteLine(" --- {0}", s.FullName); 
}

If I wanted to search for a specific last name, first name, teacher, or whatever property there may be in our “StudentsCollection” object, I could replace .Grade and go from there.  Now, what if I didn’t want the entire object returned?  I’m just displaying the FullName property of the object—why return all that extra data?

var ninthGraders = from s in students 
    where s.Grade == "09" 
    select s.FullName;  

foreach (var s in ninthGraders) 
{ 
    Console.WriteLine(" --- {0}", s); 
}

Since our ‘var’ accepts whatever we’ve placed into it, it is now a collection of Strings rather than a collection of StudentRecord objects.

Tags: , , ,

%d bloggers like this: