Query the feed document and use a For Each loop and invoke the DataContext.Add() method to insert each object into the DataContext. Finally apply the DataContext.Submit() method to persist the collection.

Early-bind element names and execute a LINQ query to generate an IEnumerable(Of PostItem) collection of projections created with object initializers. This eliminates the need for the For Each loop.

LinqDataView is an internal object from the System.Data.DataSetExtensions namespace that inherits DataView and implements LinqDataView, IBindingListView, IList, ICollection, and IEnumerable. Implementing the IBindingListView interface enables advanced (multi-column) sorting and filtering, as mentioned in my September 7, 2007 Support Sorting and Filtering for List<T> Collections post. You create a LinqDataView instance when you invoke the AsDataView() method on a LINQ to DataSet query.

Andrew's Science Project post of September 7, 2007 includes the source code for a LINQ to DataSet extension method that removes the CopyToDataTable<T> method's restriction of the generic T type parameter to DataRow. The extension method enables loading instances of anonymous types into DataRows.

Daniel Moth has a July 27, 2007 LINQ to DataSet item with details of the classes in the System.Data.DataSetExtensions namespace.

Here are links to Erick Thompson's three introductory LINQ to DataSet posts from February 2007: Part 1, Part 2 and Part 3.

Added: October 12, 2007, 0930 PST

Frans Bouma Moves from Days to Parts and Admits Matt Warren Was Right About Sets

Object graphs are DAGs and Frans mentions that "cycles in relational models which are reflected in entity models is a Bad Thing and typically have to be redesigned." I mention the issue with cycles in serializing entities generated from LINQ to SQL that have both m:1 and 1:n associations in my LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 of October 9, 2007.

Update October 15, 2007: Frans clarifies in a comment that he's referring specifically to cycles in relational models rather than cycles in related object graphs. The latter topic is one of the subjects of my XML serialization issues posts.

Frans then equates his earlier DAT and Topological Sorting foible with the requirements for handling a multiplying number of special cases in his previous (day 5) post, concludes that Matt Warren's following recommendation in a comment to use sets was right.

Joe Cheng Solves the Wide Finder Problem with LINQ and PLINQ

Tim Bray's Wide Finder Project originated as an exercise to improve the performance of a small Ruby program that reads Apache logfiles, applies a regular expression, counts, sorts, and finally prints the 10 most popular blog posts. The challenge is to take advantage of the CPU's multiple cores by using parallel processing to improve performance.

Joe Cheng, a developer on the Windows Live Writer team, posted on October 2, 2007 Wide Finder with LINQ, which contains a compact LINQ query with a few lines of C# code that beats the performance of the Ruby by a couple of seconds when reading a million-line logfile.

A pure C# version beat the LINQ query by a couple of seconds, but required 85 lines of code. As Joe notes, the LINQ version has the potential to use Parallel LINQ (PLINQ) from the forthcoming Parallel FX Library to improve performance.

The obvious advantage of moving from "conventional" C# 3.0 constructs to a single LINQ query is Parallel LINQ (PLINQ, which is part of the Parallel FX or PFX program) compatibility for simplified parallel processing on multi-core machines. There's a substantial Microsoft team at work on PLINQ and the October 2007 issue of MSDN Magazine carries a "Running Queries On Multi-Core Processors" story about PLINQ by Joe Duffy and Ed Essey.

Note: Don Box published in Wide Finder in C# - the Naive implementation of October 9, 2007 a more complex LINQ query that reads a stream from the logfile but appears to be slower than Joe Cheng's version (which Don cites in a ednote update.)

Added October 11, 2007, 0800 PST: I found this jewel thanks to Tim Bray's WF IX: More, More, More post of October 10, 2007)

Beth Massi Conducts Channel9 Sync Services Interview with Milind Lele

This blog also covers SQL Server CE and Microsoft Synchronization Services (a.k.a. Occasionally Connected Systems or OCS) topics, which don't see as much action as LINQ and the Entity Framework. Milind is a program manager on the VB Team, so you get to see some VB code instead of Sync Guru Rafik Robeal's C#-only samples.

Julie Lerman Describes How to Customize Your Astoria Data Source

Her Custom Operations and hiding entities in Astoria post of October 10, 2007 shows how to "customize your service by providing explicit query operations and also blocking specific query operations." She also notes that you can build read-only views into the service, but you'll have to "wait wait until we have bits that are not just prototype" to learn how to enable stored procedures.

The unbound DataContext provides Attach(), AttachAll(), Add(), AddAll(), Remove()and RemoveAll() methods for Table<TEntity> implementations of the ITable interface and stores the original and changed entity values. You apply changes to the attached entities directly. The Table<TEntity>.GetModifiedMembers() method contains references to added, removed, and modified members.

Invoking Mike's GetInserted(), GetModified() and GetDeleted() methods supply modified argument values to InsertEntity(modified, original) and UpdateEntity(original, modified) service methods. The Table<TEntity>.GetOriginalEntityState() method provides original argument values to the preceding two service methods, plus the DeleteEntity(original, modified) method. (DeleteEntity() requires both arguments because you must attach the entity before you remove it.)

Mike includes a simple client console app to test his methodology, which he says "is really a bit subversive and possibly a bit evil."

What's interesting about this approach is its similarity to Matt Warren's promised "mini connectionless DataContext" that I discussed in my Changes Coming to LINQ to SQL post of May 15, 2007. This article resulted in a long LINQ: Next CTP thread in which Matt Warren reneged with these two posts:

No, this feature will not be in the first release of LINQ to SQL. ...

It is only missing because of lack of time. When I mentioned it, I was talking about our forward thinking and a sample I'm trying to put together.

Note: Mike uses the terms Shared Contract and Shared Types differently than the WCF team. Sowmy Srinivasan describes the difference between the two terms in his All About KnownTypes post of June 06, 2006 by how they're serialized on the wire. Aaron Skonnard discusses the history of WCF SharedContract and SharedType modes in his WCF's NetDataContractSerializer post of April 21, 2006. Services that use the default DataContractSerializer implement the SharedContract mode.

Added: October 10, 2007, 1100 PST

Update October 11, 2007: Mike's DurableServices in WCF V3.5 :-( post today indicates to me that he's working on another approach or a supplement to maintaining context with WCF features akin to Durable Services.

Update October 14, 2007: Corrected service method names and clarified client-side operations in conjunction with the

This post introduces the issues I encountered when attempting to create a sample application that illustrates interactions between a real-world, data-bound Windows client, Windows Communication Foundation (WCF), and LINQ to SQL data access layer (DAL).

The post deals with the full lack of object-graph schema fidelity to the original entity's properties. You lose Attached Associated Objects Bugs for m:1 associations if your object has EntitySets for 1:n associations that share a common property.

There's also a graphic example of the "Attached Associated Objects Bug" that duplicates associated entities when attaching a Table<TEntity> object that has EntitySet properties to a DataContext.

Added: October 9, 2007, 1700 PST

Matt Warren Tackles the Four OrderBy Flavors in Part VIII of His Building an IQueryable Provider Saga

Fortunately, there is only one way to do ordering, and that's using the LINQ ordering specific query operators. The bad news is that there are four different operators.

Matt then runs into (and around) corners resulting from the immutability of expression trees and devises an amazing OrderByRewriter class that he describes as "a whole lot of something!" to handle what appears to be this simple LINQ query:

"Leverage LINQ in ASP.NET 3.5 Projects" Available for Download from WROX

My Leverage LINQ in ASP.NET 3.5 Projects article is one of four inaugural Wrox Blox downloadable "informational pieces" that "help get you up-to-speed and keep you up-to-date" plus "build the knowledge you need quickly on a wide variety of topics."

For $3.99, you can download the 38-page tutorial and it's accompanying C# and VB source code for a complete LINQ to SQL data access layer (DAL). The DAL binds to a LinqDataSource or ObjectDataSource control and an updatable GridView control on ASP.NET 3.5 pages. (The source code requires Visual Studio 2008 Beta 2 or later and the Northwind sample database installed on an SQL Server 2005 Express instance.)

You can add a new "BOGUS" Customer entity with code, edit its properties in the grid, and delete the entity without affecting your original Northwind Customers table's contents.

Click image for full-size screen capture.

The source code for the LinqDataSource and ObjectDataSource test harness is available here.

Catch 10 LINQ-Related Sessions at Tech*Ed Developer 2007

Tech*Ed Developers 2007 to Include 10 LINQ-Related Presentations includes abstracts and presenter names of the 10 sessions in Tech*Ed 2007 Barcelona being held on November 5 - 9, 2007. LINQ Luminaries Luca Bolognese, Mike Taulty, and Bart De Smet are among the presenters. This post eliminates the need to register to open and search the EMEA Events page for the conference.

Dinesh's Attach() if you have something detached post of 10/8/2007 attempts to clear up the confusion surrounding use of the DataContext.TableName.Attach() method in stateless ASP.NET and Windows Communication Foundation (WCF) service projects. Dinesh attributes much of the confusion to the term detached object support, which he characterizes as a "misnomer." As an example, Dinesh cites

Detached Objects – Instances that lose association with persistence manager when you close() the session

Hibernate allows for selective reassociation of a subgraph. [For example,] you may serialize objects to the web tier, then serialize them back to the EJB tier in the next request.

Reference to detached object outside of a transaction/persistence manager can beretained and reassociated with a new persistence manager.

Note: For more about detached objects in Hibernate, see section 4.1.3 Detached objects on page 142 (Chapter 4) of Hibernate in Action by Christian Bauer and Gavin King (Manning, 2005).

Hibernate and NHibernate are mature, widely used, and the standard of comparison for O/RM tools. What's interesting is that LINQ to SQL doesn't support selective reassociation of a subgraph or reassociation with a a new persistence manager. Reassociation appears to be explicitly verboten (by exception) in the RTM version.

Perhaps the confusion about detached objects arises from differences in definition between LINQ to SQL and Hibernate.

Another source of confusion might be the bug in LINQ to SQL Beta 2 that causes entities associated with a newly-attached root entity to be inserted rather than assumed to be persisted in the database and awaiting update. For more details, see Feedback: Incorrect Update Behavior - Attaching an Entity after Calling a Relationship Property of August 27, 2007 (resolved as fixed on September 5, 2007) and Rick Strahl's Complex Detached Entities in LINQ to SQL - more Nightmares post of October 1, 2007. If the LINQ to SQL team delivered their long-promised sample project to demonstrate LINQ to SQL with WCF services, they might have discovered the issue before locking down the Beta 2 bits. (This assumes that the example included associated entities, which seems to be uncommon in sample projects.)

Note: See sample WCF project issues with LINQ to SQL and Entity Framework in my "From the 'Talk is Cheap' Department" subhead of the "Julie Lerman Demonstrates Detached Updates with Entity Framework" of LINQ and Entity Framework Posts for 9/28/2007+ post.

Stay tuned for a LINQ to SQL/WCF test harness that demonstrates this bug.

One saving grace is that objects stored in viewstate get updated automagically during postback. In other words, if I create a cust object and store it in viewstate, then make a change to the cust object, then postback and pull the object back out of viewstate, it will have the newer property values. Viewstate is synced up to the object.

3
comments:

Frans Bouma
said...

"I mention the issue with cycles in serializing entities generated from LINQ to SQL that have both m:1 and 1:n associations in my LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 of October 9, 2007."No, that's not the same thing :). What you mentioned is a problem for graph handling, and one of the reasons why my own algorithm didn't work that well.

What I meant with cycles is this:You have a Department entity which has a 'Manager' FK to 'Employee'. You also have a 'WorksFor' FK in employee to Department.

This is a cycle. You can't save them with 2 insert statements, you need 2 insert statements and 1 update. (insert NULL in one of the two FK fields). This is the reason why it's bad.

What YOU mentioned is a cycle in the in-memory graph, but that's actually artificial. I.e.: the relation Customer - Order (1:n) is the same as the relation Order - Customer (m:1), they're based on the same fundamental association between order and customer, namely based on the FK in order to customer, and nothing else.

So the graph in memory has cyclic references, but the ENTITIES don't have a cycle, the dependency order -> customer is only one way, so a directed graph. (my algo tagged entities already seen to avoid the cyclic references, which failed in some edge cases where multiple paths ran through the same in-memory object)

The dual Web role application has been running in Microsoft's South Central US (San Antonio) data center since September 2009. I believe it is the oldest continuously running Windows Azure application.

About Me

I'm a Windows Azure Insider, a retired Windows Azure MVP, the principal developer for OakLeaf Systems and the author of 30+ books on Microsoft software. The books have more than 1.25 million English copies in print and have been translated into 20+ languages.

Full disclosure: I make part of my livelihood by writing about Microsoft products in books and for magazines. I regularly receive free evaluation software from Microsoft and press credentials for Microsoft Tech•Ed and PDC. I'm also a member of the Microsoft Partner Network.