Tuesday, January 29, 2008

LINQ and Entity Framework Posts for 1/28/2008+

Note: This post is updated daily or more often, depending on the availability of new articles.

Designing LINQ to Entities Queries that Deliver Entities, EntitySets and/or EntityReferences

The "correct" method for eager-loading entities' EntitySets and/or EntityReferences is to add an Include("EntitySetOrRefName") to the query, as in

var query = ctxNwind.Orders
            .Include("Customers")
            .Where(o => o.Customers.CustomerID == customerIDTextBox.Text)
            .OrderByDescending(o => o.OrderDate);

to return a Customer EntityReference for each Order in the sequence. The obvious issue with the Include() method is that you lose compile-time checking of the LINQ to Entities expression by late-binding the related entity name.

However, the Entity Framework Sample Query Explorer project that installs with the Entity Framework Beta 3/Entity Framework Tools CTP 2 doesn't use the Include() method in the six sample queries (LinqToEntities70 to LinqToEntities76) of the Entity Framework Sample Query Explorer | Linq To Entities | Relationship and Navigation node.

Relationship Collection 1 uses the following query:

public void LinqToEntities70()
{
    var query = context.Customers
                .Where(cust => cust.CustomerID == "ALFKI")
                .Select(c => c.Orders
                .Select(o => o));

    foreach (var order in query)
    {
        ObjectDumper.Write(order);
    }
}

to "Select a sequence of all the orders for a customer using Select." This query shouldn't work because it doesn't invoke the .Include("Orders") method for implicitly eager-loading each Customer entity's Orders EntitySet. (According to Julie Lerman, Danny Simmons considers the working query to be a bug.) The "correct" form of the query is:

var query = ctxNwind.Orders
            .Include("Customers")
            .Where(o => o.Customers.CustomerID == customerIDTextBox.Text)
            .OrderByDescending(o => o.OrderDate);

My February 1, 2008 Entity SQL Queries for Retrieving EntityReferences and EntitySets post (updated February 2) has more on this issue, including performance measurements.

Added: February 2, 2008

Keyvan Nayyeri Builds a Blog Engine with ASP.NET MVC and LINQ

Iranian developer Keyvan Nayyeri has started a multi-part series with these two parts:

Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 1 of January 30, 2008:

Microsoft released the first CTP of ASP.NET 3.5 Extensions and it includes ASP.NET MVC Framework as one of the main extensions for ASP.NET 3.5. In the first part of this article series about building a simple blog engine with ASP.NET MVC and LINQ, Keyvan introduces the MVC pattern, ASP.NET MVC Framework, and the fundamentals of a simple blogging engine.

Part 1 covers;

Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 2 of the same date:

In the second part of the article series about ASP.NET MVC Framework, Keyvan adds controllers to his blogging engine in order to describe how to use controllers in ASP.NET MVC and discusses some details related to controllers. He first discusses the concept of URL routing patterns and then explores the anatomy of a controller class. Finally, he examines how to implement the controllers in his sample blog application.

Part 2 includes the following topics:

Note: Kevin was born in Kazakhstan but now lives in Tehran.

Added: February 1, 2008

Stephen Toub Posts Essay on Recursion and Concurrency with PFx and PLINQ

The Parallel Programming with .NET blog's Recursion and Concurrency post by Stephen Toub of January 31, 2008 discusses scenarios for parallel processing of tree data structure traversal. Processing trees with multiple threads creates a number of issues, especially with recursive tree walking. The TaskParallel library simplifies walking the tree in parallel with a method such as this:

public static void Process<T>(Tree<T> tree, Action<T> action)
{
    if (tree == null) return;
    Parallel.Do(
        () => action(tree.Data),
        () => Process(tree.Left, action),
        () => Process(tree.Right, action));
}

To minimize system overhead, you can specify sequential process after reaching a specified depth in the tree:

private static void Process<T>(
    Tree<T> tree, Action<T> action, int depth)
{
    if (tree == null) return;
    if (depth > 5)
    {
        action(tree.Data);
        Process(tree.Left, action, depth + 1);
        Process(tree.Right, action, depth + 1);
    }
    else
    {
        Parallel.Do(
            () => action(tree.Data),
            () => Process(tree.Left, action, depth + 1),
            () => Process(tree.Right, action, depth + 1));
    }
}

The author concludes:

All in all, parallelism is a very interesting problem when it comes to recursion, and we hope the Parallel Extensions to the .NET Framework go a long way towards making the merging of these two concepts significantly easier.

Added: February 1, 2008

Bill Horst Tackles Left and Right Outer Joins with LINQ to Objects and LINQ to SQL

The VB team's Converting SQL to LINQ, Part 8: Left/Right Outer Join (Bill Horst) post of January 31, 2008 shows you how to emulate Left and Right Outer Joins with Group Join statements and Select clauses with nested IIf() function calls for LINQ to Objects. Substituting LINQ to SQL as the data source requires modification of the IIf() functions and an explicit cast to Nullable(Of OrderDate) for his Customer/Orders example.

Added: January 31, 2008

Julie Lerman Morphs Brad Abrams' and Lance Olson's MVC with EF Demo to AdventureWorks, VB, and Streamlined LINQ Queries

It didn't take Julie long to rework Brad and Lance's Mode-View-Controller C# 3.0 walkthrough with Entity Framework as the data source to run with AdventureWorks and VB 9.0. She posted her MVC with Entity Framework ... a twist on Brad Abram's example example at 3:04 PM EST on January 31, 2008, about 50 hours from the original ASP.NET MVC Example Application over Northwind with the Entity Framework post on January 29 at 10:14 AM PST (1:14 PM EST). [See the "Brad Abrams Posts Step-by-Step Details for Using EF with ASP.NET's MVC" topic below.]

Julie takes advantage of her LINQ to Entities experience to use a little-known syntax for returning child sequences:

var _prod = Northwind.Categories.
            Where(c => c.CategoryName == id).
            OrderBy(c => c.CategoryName).
            Select(c => c.Products).
            First().ToList(); 

The preceding query differs from the one I used with Northwind in the NwindEdmCS.sln sample code for my "Model Domain Objects with the Entity Framework" cover story for Visual Basic Magazine's March 2008 issue to populate the updatable orderDataGridView:

var query = ctxNwind.Customers
    .Where(cust => cust.CustomerID == customerIDTextBox.Text)
    .Select(c => c.Orders
    .Select(o => o))
    .FirstOrDefault()
    .OrderByDescending(o => o.OrderDate);
orderBindingSource.DataSource = query.ToList();

I discovered this syntax from the Entity Framework Sample Query Explorer’s LINQ to Entities | Relationship Navigation | Relationship Collection 1 (LinqToEntities70) query. It appears to me that the second Select() is necessary to sort the Orders. Here's the project's vanilla UI:

 

Click above capture for full-size image.

Added: January 31, 2008

Charlie Calvert Delivers a LINQ Expression Tree Tutorial

C# community evangelist Charlie Calvert's Expression Tree Basics post of January 31, 2008 demonstrates how to create an expression tree for the lambda function (a,b) => a + b, which adds two integers, a and b and returns the sum. Charlie explains that expression trees converts code into data, such as LINQ to SQL expression to T-SQL commands, and details the steps required to write and analyze a simple C# expression tree.

Added: January 31, 2008

SD Times' "Does .NET With LINQ Beat Java?" Article Raises More Questions Than It Answers

David Worthington's Does .NET With LINQ Beat Java? article of January 30, 2008 has a snappy headline but the article's deck, "Framework's data query capabilities give it an edge, experts claim," appears to be disputed by at least one "expert," namely Jonathan Bruce of DataDirect Technologies.

I attempt to avoid ad hominem posts but I couldn't let Jonathan's innuendo go unanswered. My response is here.

Added: January 31, 2008

Jonathan Bruce's Data Services and LINQ Crystal Ball Appears a Bit Cloudy

Julie Lerman's Jonathan Bruce of DataDirect's 2008 Predictions of 1/31/2008 listed his following predictions on the data access front in 2008:

  1. Data Services changes becomes universal programming model.
  2. IBM files a draws together a set of established JDBC experts to establish LINQ for Java, Java Specification Request (JSR)
  3. Dynamic LINQ bridges gap between compile-time type checking available in more static LINQ constructs.
  4. LINQ to SQL will be forced to open up their lighter weight model to third-party provider-writers.

According to Software Development Times magazine's Does .NET With LINQ Beat Java? article of January 30, 2008 by David Worthington [see above], Jonathan's "the program manager for .NET technology group at DataDirect Technologies, and formerly JDBC specification lead and architect for the Java platform at Sun."

So I thought something might have been lost in translation between Jonathan's Predictions for 2008? post of January 29, 2008 and Julie's copy. No such luck; Julie's quotes were identical, even to the missing the period in #2.

I can't fathom the first three prognostications, so here's my request for clarification:

1. What "Data Services changes will becomes [sic] the universal programming model?" ADO.NET Data Services? If not, what or whose "Data Services changes?" What's "the universal programming model." Is it (to paraphrase Ted Neward) the "One Programming Model God Intended" to silence the Tower of Programming Model Babel?

2. Will "IBM file ... a set of established JDBC experts" (sounds impractical) or "draw [them] together? The latter sounds like herding cats to me. Will LINQ to Java have any relationship to IBM's pureQuery (formerly JLINQ)? JLINQ appeared to me (and others) to be a lightweight code generator for Java classes that's similar to LINQ to SQL without LINQ.

3. "Dynamic LINQ bridges [what] gap between compile-time type checking" and something else? In my view, you either have compile-time checking or you late-bind SQL strings, which is what LINQ eliminates with LINQ to SQL and LINQ to Entities. I'm confused.

4. "LINQ to SQL will be forced to open up their lighter weight model to third-party provider-writers" when hell freezes over. Microsoft is placing its database-agnostic bets on the Entity Framework and isn't about to devote the resources needed to write expression trees for PL/SQL or DB2 SQL dialects. Data Direct has announced plans to offer EntityClient-enabled ADO.NET data providers for the Entity Framework.

Here's hoping for a bit of defogging of Jonathan's scrying.

Added: January 31, 2008

Elisa Flasko Publishes "Introducing LINQ to Relational Data" to the MSDN Library

Elisa answers the question I posed last week about LINQ to Relational Data becoming LINQ to Entities' new name with her "Introducing LINQ to Relational Data" white paper of January 30, 2008. LINQ to Relational Data is a euphemism for two implementations of LINQ: LINQ to SQL and LINQ to Entities. Elisa provides a basic tutorial for creating a LINQ to SQL DataContext and Entity Data Model (EDM) with a LINQ to Entities query. She provides feature checklists to suggest when to use LINQ to SQL or the Entity Framework with LINQ to Entities.

Added: January 31, 2008

Marcelo Lopez Ruiz Explains Exception Handling for ADO.NET Data Services

The current ADO.NET Data Services drop returns exception messages and a stack trace to the client the service throws an exception. As Marcel says, this level of detail represents a potential security problem, so this will be changed in a future CTP.

Marcel mentions that you can throw the WebDataServiceException to return a custom error message with a 400 - Bad Request response. The "Using ADO.NET Data Services (“Project Astoria”)" white paper provides an example of its use.

Marcelo promises more information about exceptions in his next post.

Added: January 30, 2008

Brad Abrams Posts Step-by-Step Details for Using EF with ASP.NET's MVC

Brad's ASP.NET MVC Example Application over Northwind with the Entity Framework post of January 29, 2008 describes in detail how to create a ASP.NET Model-View-Controller project with Entity Framework, rather than LINQ to SQL as the back end. In addition, Brad and Lance Olson include a few sample unit tests at the end of the walkthrough.

You can download the complete source code here.

Thanks to Guy Burstein for the heads-up.

Added: January 30, 2008

Logging EntityBag's SOAP Request and Response Messages

If you're working with Danny Simmons' EntityBagSample.sln project, you might want to change the security model and enable message logging to test the effect of applying WS-* message-level security to your test project. My Logging EntityBag's SOAP Request and Response Messages post of January 29, 2008 shows you how to do this and how to associate Web_message.svclog log files with the ServiceModel Trace Viewer utility (SvcTraceViewer.Exe.)

Frans Bouma Reaches the Twelfth Step in His Quest for LINQ to LLBLGen Pro

In his Developing LINQ to LLBLGen Pro, part 12 post of January 29, 2008 Frans tackles Cast, OfType, Except, Intersect, and Single Standard Query Operators, examines the 'as' and 'let' keywords, and discusses LINQ to SQL issues with the T-SQL DISTINCT operator. Finally Frans described the work remaining in his LINQ to LLBLGen Pro implementation.

Frans is further down the home stretch.

Julie Lerman Test-Drives Xceed's DataGrid for WPF with  Entity Framework

Julie provides the step-by-step details for hooking up the XCeed Data Grid for WPF to a LINQ to Entities IQueryable<SalesOrderHeader> query result in her Binding EF data to the (free) Xceed WPF Data Grid post of January 28, 2008. She wasn't able to verify the grid's sorting features but does demonstrate it's grouping capabilities.

WPF in .NET 3.x doesn't have a built-in data-bindable grid control. If you haven't checked out third-party data grids or other data-bound controls for WPF, give Xceed's DataGrid for WPF Live Explorer demo a try.

Note: Xceed offers a free license for the DataGrid Express Edition, which requires free registration. The Professional Edition license is $495.

Lang.NET Symposium v2 Free of LINQ Presentations(!)

The second Lang.NET Syposium kicked off on January 28, 2008 in Redmond. Surprisingly, there were no presentations on LINQ, but Ted Neward's Highlights of the Lang.NET Symposium, Day One post reviews Anders Heljsberg's C# presentation:

Anders walks us through the various C# 3.0 features and how they combine to create the subtle power that is LINQ (it's for a lot more than just relational databases, folks), but if you've seen his presentation on C# 3 at TechEd or PDC or any of the other conferences he's been to, you know how that story goes. The most interesting part of his presentation was a statement he made that I think has some interesting ramifications for the industry:

"I think that the taxonomies of programming languages are breaking down. I think that languages are fast becoming [an] amalgam. ... I think that in 10 years, there won't be any way to categorize languages as dynamic, static, procedural, object, and so on [paraphrased]."

Despite the lack of LINQ or Entity Framework content, Ted's literate and insightful review of the symposium's first day is a great read, especially if you're interested in dynamic languages.

Erik Meijer will present a Tuesday morning session on his Volta incubator project, but Volta's off-topic for this blog.

Update January 30, 2007: Ted's Highlights of the Lang.NET Symposium Day Two post describes Erik's presentation:

It's obvious why Erik is doing his talk at 9AM, because the man has far more energy than any human being has a right to have at this hour of the morning. Think of your hyperactive five-year-old nephew. On Christmas morning. And he's getting a G.I. Joe Super Bazooka With Real Bayonet Action(TM). Then you amp him up on caffeine and sugar. And speed.

Start with Erik's natural energy, throw in his excitement about Volta, compound in the fact that he's got the mic cranked up to 11 and I'm sitting in the front row and... well, this talk would wake the dead.

Erik demonstrates the same enthusiasm when speaking about LINQ or Haskell.

Ted also provides detailed coverage of Paul Vick's presentation: "He's talking on 'Bringing Scripting (Back) to Visual Basic', something that I can definitely agree with" and goes on to deplore the unjust denigration of VB by adherents of the "One Language God Intended":

Whatever you did, VB, your punishment couldn't have fit the crime. Hopefully your highly-publicized personal hell is almost over.

Ted says that videos of the presentations should be available on Channel9 in about a week.

Update January 31, 2007: Ted's Highlights of the Lang.NET Symposium Day Three post attests to his almost-photographic memory, because his Mac froze when he hooked it up to a projector for a quick Scala presentation and his notes went bibi. (Serves you right for using a Mac, Ted.)

Danny Sullivan Posts Complete EntityBag Test Project with Bug Fix

Danny supplemented his earlier Perseus class library, which you need to implement his EntityBag class for service-enabling Entity Framework projects, with an EntityBagSample.sln project that you can download as Perseus-1.1.zip from his new Perseus: Entity Framework Entity Bag site on the MSDN Code Gallery. The new code, updated on January 27, 2008, fixes a minor bug "with connection strings that use Name to look up values from the config file."

Stefan Cruysberghs Starts Introductory Entity Framework and LINQ to Entities Series

Remember when LINQ to SQL was newly released and almost every technical blogger with a penchant for .NET was competing with Scott Guthrie's LINQ to SQL tutorials?

The cycle's starting again with the Entity Framework and LINQ to Entities.

Stefan Cruysberghs, who previously published a four-part .NET - LINQ to SQL tutorial, posted a detailed .NET - ADO.NET Entity Framework & LINQ to Entities - Part 1 episode on January 27, 2008.

Mike Pizzo to Present Programming LINQ and the Entity Framework Webcast on January 30, 2008

ADO.NET principal architect Mike Pizzo will present a one-hour live webcast, Programming LINQ and the Entity Framework, at 11:00 AM PST on Wednesday January 30, 2008. According to the webcast registration page:

Language Integrated Query (LINQ) introduces an exciting new way for applications to build strongly typed queries that are deeply integrated into the programming language. The ADO.NET Entity Framework allows applications and services to work in terms of an application-oriented Entity Data Model, decoupling the application's data model from the storage considerations of the relational schema. Join this webcast to see how these two technologies work together to change the way applications work with data.

Jim Wooley Updates His ThinqLinq Site

Jim spent Sunday, January 27, 2008, adding new features and posts to his LINQ-Powered ThinkLinq site. Check out:

New MSDN Code Gallery Offers LINQ and Entity Framework Content

The newly established MSDN Code Gallery has opened with several recycled LINQ examples and copies of earlier LINQ-related blog posts. The Entity Framework content is described in last week's LINQ and Entity Framework Posts for 1/21/2008+ post.

Charlie Calvert describes the differences between MSDN Code Gallery, CodePlex, and MSDN Dowloads in his Code Gallery goes Live: New Site for Samples of January 28, 2008.

I've avoided the temptation (so far) to upload old articles from the OakLeaf blog, but I might try uploading recent ones.

Suggestion: The home page and individual items have RSS feeds; individual Popular Links don't but should.

Latest Third-Party LINQ Implementation: LINQ to Named Pipes

An exception to the redundancy noted above in the MSDN Gallery's LINQ topics is an example of a LINQ to Named Pipes implementation posted by Charlie Calvert.

0 comments: