Tuesday, July 7, 2009

RemObjects DataAbstract DALinq

I went to the local library yesterday to see if they had a book on Windows Presentation Foundation (yes, I'm that cheap). They didn't have one, but did have a book on LINQ which I've been meaning to look into more. Since I use RemObjects DataAbstract I was very interested in playing with their DALinq features. All I have to do is write the LINQ query and the SQL will get generated on the server for me sending back the correct data. So let's get started with the project.

I ran through the standard C# WinForms DataAbstract project and pointed it at the Northwind database in my SQL Server 2005 install. After going through the wizard my first step was to figure out why I didn't have a LinqRemoteDataAdapter in my Visual Studio 2008 toolbox. Well, before you start a DALinq project make sure you don't have it set up to create a .NET 2.0 compatible project (which is what I normally create). I set the project options for my client to .NET 3.5 and got to work.

First I removed the standard RemoteDataAdapter and replaced it with the LinqRemoteDataAdapter. Then I selected the LinqRemoteAdapter's Smart Tag and selected "Create DA LINQ Classes" from the menu.



I then added System.Linq and RemObjects.DataAbstract.Linq to the using clauses. Okay, perhaps I did this after my linq query got errors and Google reminded me that the using clauses are important. ;-) My Linq query was pretty simple for this sample and was just put behind a button that made the query and then displayed it in a DataGridView. It was based on the query on the feature's webpage

var aList = from c in linqRemoteDataAdapter.GetTable<Customers>()
where (c.City == "México D.F.")
select new { c.CustomerID, c.ContactName, c.Address };

dataGridView1.AutoGenerateColumns = true;
bindingSource1.DataSource = aList;


Or, if you prefer dot notation:

var aList = linqRemoteDataAdapter.GetTable<Customers>()
.Where(c => c.City == "México D.F.")
.Select(c => new { c.CustomerID, c.ContactName, c.Address });


I did have to adjust the original query which was taken almost directly from the webpage and looked like this:

var aList = from c in Customers
where (c.City == "México D.F.")
select new { c.CustomerID, c.ContactName, c.Address };


When I create it like that I got a squiggly line under "Customers" that says "Could not find an implementation of the query pattern for source type 'DALinqClient.Customers'. 'Where' not found." When I consulted the sample DALinq project that ships with the project I changed the code to use the GetTable command they used and it worked great!



As I learn more about Silverlight I'm looking forward to being able to use this feature for my data access. At least with Silverlight 2 (I haven't kept up much with v3) this will make data access MUCH easier. The project (as simple as it is) can be found here.


About The Author

Ron Grove draws on over ten years of training, network administration and development experience. He loves to work with new technology and see how that technology can be best utilized by his clients. You can find him through his company Evanoah, LLC and his LinkedIn profile is here.

0 comments:

Post a Comment