Browse > Home / Archive by category 'LINQ'

| Subscribe via RSS

Super Lambda Bananas

June 29th, 2008 | No Comments | Posted in C#, LINQ

302279198_06564a7141_m I’m a big generic collection user and I can’t express how much the C# 3 Linq expressions have improved my coding experience, especially in the form of lambda syntax. I used to spend a lot of time trying to bend predicates to my will in order to pull the good stuff out of my collections, and some of the new expressions make this a thing of the past.

Some of the expressions do need a bit of extra thought to understand what is going on though. For example, what is the difference between .Where, .TakeWhile and .SkipWhile? They all return a subset of your collection, but what exactly do you get? Let’s investigate!

So say we start with this:

string[] names = {“dave”, “dee”, “dozy”, “beaky”,
“mick”, “titch”, “darius”};

names.Where(name => name.StartsWith(“d”));

 .Where will return “dave”, “dee”, “dozy” and “darius”, matching everything that starts with ‘d’. TakeWhile and SkipWhile are different though, working on your sequence only until a specified condition is deemed false. So…..

names.TakeWhile(name => name.StartsWith(“d”));

will return “dave”, “dee” and “dozy”: the search is called off when StartsWith(”d”) becomes false. Conversely

names.SkipWhile(name => name.StartsWith(“d”));

 

will get you “beaky”, “mick”, “titch” and “darius”, skipping the items in the sequence until StartsWith(”d”) becomes false.

Try doing that with predicates!

ADO.NET Entity Framework Quickstart Tutorial

June 3rd, 2008 | No Comments | Posted in ASP.NET, C#, EntityFramework, LINQ

The ADO.NET Entity framework received another update last week as part of the Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta, so to coincide with this I give you a quick run through to get you started. Being honest, if you have used an OR mapper in the past this will be familiar territory. If not, this new implementation is a good place to start.

screenshot1 So to begin, create a new ASP.NET website, Add New item, and then choose ADO.NET Entity Data Model. Name it EntityModel.edmx and click Add to create a new model.

The Entity Data Model Wizard will pop up and give you a chance to configure your new model. For the purposes of this walk through, choose Generate from database and click Next.

screenshot4 Set up your data connection, I won’t go into this as it’s simple enough to figure out if you’ve never done it before. Click the checkbox to save entity connection settings into your web.config and name it MyEntities.

screenshot6The wizard will then tootle off and retrieve the names of all the tables, views and stored procedures in your database. Choose a table ( I’ve chosen a table from my DB called ‘Log’ ) and name the Model Namespace MyModel.

Click Finish and the designer for your model will open. As a side note have a look at the Model Browser on the right hand side - it looks a bit ‘rendered’ for want of a better description, as though it’s drawn via GDI+ compared to the Solution Explorer….strange!

Now add a new web page and go into the code behind to start the real work. It’s just a simple bit of code to CRUD the Log entities in my database.


// instantiate a new Log entity
MyModel.Log log = new MyModel.Log();

// populate it’s properties
log.Date = DateTime.Now;
log.Exception = “Test Exception”;
log.Level = “Test Level”;
log.Logger = “Test Logger”;
log.Message = “Test Message”;
log.Thread = “Test Thread”;

//instantiate the entity ‘context’ - the object used
//as a ‘gateway’ to the DB
MyModel.MyEntities entities = new MyModel.MyEntities();

// Save the new log entity to the DB
entities.AddToLog(log);
entities.SaveChanges();

// Load it back via a little LINQ query
// ( funny how you must use .First instead of .Single )
MyModel.Log loadedLog = entities.Log .Where(ent => ent.Level == “Test Level”).First();

// make a change and save it back to the DB
loadedLog.Message = “Message has changed!”;
entities.SaveChanges();

// Finally delete the object from the DB
entities.DeleteObject(loadedLog);
entities.SaveChanges();

Notice how you have to call .SaveChanges() to persist back to the DB. That’s it! A super fast run through of CRUD with the ADO.NET Entity Framework! Enjoy!

kick it on DotNetKicks.com

LINQ - First Impressions

August 20th, 2007 | No Comments | Posted in Framework, LINQ

LINQ is pretty good! I’ve used plenty of ORM tools in the past, and LINQ offers a nice user experience. I spent an hour hooking up Northwind and dragging tables onto the design surface in order to generate some code and the created objects offer some cool features.
The LINQ syntax does feel a bit like writing inline SQL though, and doesn’t feel as ‘objecty’ as the entities thrown up by Subsonic. The use of the DataContext classes *seem* to offer transactions out the bag, and the hierarchy of methods and properties feel well thought out and usable.

I’m going to do a little ‘Learn LINQ’ series of posts later in the week, so stay tuned.