Tuesday, May 15, 2007

Changes Coming to LINQ for SQL

Beta 2 will bring a substantial number of changes to Orcas's LINQ-related features. Following are a few of the forthcoming changes mentioned in the LINQ Project General and ADO.NET vNext forums.

Simplifying Data Tracking in n-Tier Projects

Matt Warren replies to questions in the very long LINQ Project General forum's Update Existing Disconnected Objects thread (4/12/2007):

There is more work coming that makes 3-tier scenarios easier. The [current] re-attach and playback API's are included because they are the bare minimum required to make any reconnecting work at all, however cumbersome it may seem at this time.

  • There is an upcoming feature that automatically databinds LINQ to SQL objects directly to webforms that use these API's to communicate changes back into the DataContext.
  • There is also a technology in the works that automatically serializes LINQ to SQL objects to other tiers, change tracks and data binds the objects for you on that tier and serializes the objects back, solving the cyclic reference serialization problem and the indescribable schema problem for objects with change sets; which seems to match what many of you are describing.

The mechanism that does the change tracking on the client is similar to a mini-connectionless DataContext. It is a type that packages up all the objects, lists of objects and graphs that you want to send to the client. It serializes itself and everything you've given it automatically. (It implements IXmlSerializable.)

  1. On the client, the same class also manages change-tracking for all the objects that were serialized with it.
  2. When serialized again (on the trip back) it serializes both the objects and their change information that it logged.
  3. Back on the middle tier, you just make one call to re-attach the whole package to a new DataContext instance and then call SubmitChanges.

[Formatting added.]

I'm looking forward to test-driving the mini-connectionless DataContext when Orcas Beta 2 arrives (sometime between July and September, according to Microsoft sources).

Update 6/28/2007: The "mini-connectionless DataContext" that Matt Warren described in his April 12, 2007 post to the Update existing disconnected objects (dlinq) thread in the LINQ Project General forum won't be in VS 2008's LINQ to SQL implementation. See Matt's June 18 and 27, 2007 posts to the LINQ: Next CTP thread and my Rico Mariani's DLinq Performance Tips (Part 2) and Compiled Queries post of 6/28/2007.

The O/RM Designer's O/R Provider Property Will be Gone in Beta 2

According to Microsoft's Jay Hickerson in an answer to questions in the forum's LINQ to SQL designer "O/R Provider" property thread:

The O/R Provider was originally placed there so that we could generate code that would work correctly with databases other than MS SQL Server at runtime. However, the runtime support for this did not materialize in time to get it into the designer for Orcas. The property has been removed for Beta 2 and there will not be support for different providers from the designer. We are considering adding some level of support back in a future release, possibly a service pack.

Lack of support for databases other than SQL Server is the subject of my recent Future LINQ to SQL Support for Multiple Databases? post.

LINQ to SQL Integration with Windows Communication Foundation

Matt Warren says the following about WCF DataContract and DataMember attributes in answer to a question about DLINQ as WCF service in the LINQ Project General forum:

A future version of SqlMetal.exe will generate DataContract and DataMember attributes on your entities for you. Putting them on the DataContext won't work because the DataContext is not serializable.

[Thanks to Julie Lerman for the preceding link.]

Jim Wooley provides detailed instructions for adding WCF attributes as Custom Attributes properties with the LINQ to SQL O/RM tool's Attribute Editor dialog. Adding attributes in the designer prevents them from disappearing when you refresh the partial class code.

Microsoft should provide complete, non-trivial sample LINQ to SQL and Entity Framework applications that take full advantage of WCF in the Beta 2 timeframe.

Repairing the Union Operator

The following T-SQL code demonstrates with the Northwind Customersa and Suppliers tables how to write UNION queries with literals in SELECT lists based on SQL89-style equi-joins:

SELECT c.City AS City, c.CompanyName, 'Customer' AS [Type]
FROM Customers AS c, Suppliers AS s
WHERE c.City = s.City
UNION SELECT s.City AS City, s.CompanyName, 'Supplier'
FROM Customers AS c, Suppliers AS s
WHERE c.City = s.City

Executing the preceding statement generates the following output:

City      CompanyName                                    Type
--------- ---------------------------------------------- --------
Berlin    Alfreds Futterkiste                            Customer
Berlin    Heli Süßwaren GmbH & Co. KG                    Supplier
London    Around the Horn                                Customer
London    B's Beverages                                  Customer
London    Consolidated Holdings                          Customer
London    Eastern Connection                             Customer
London    Exotic Liquids                                 Supplier
London    North/South                                    Customer
London    Seven Seas Imports                             Customer
Montréal  Ma Maison                                      Supplier
Montréal  Mère Paillarde                                 Customer
Paris     Aux joyeux ecclésiastiques                     Supplier
Paris     Paris spécialités                              Customer
Paris     Spécialités du monde                           Customer
Sao Paulo Comércio Mineiro                               Customer
Sao Paulo Familia Arquibaldo                             Customer
Sao Paulo Queen Cozinha                                  Customer
Sao Paulo Refrescos Americanas LTDA                      Supplier
Sao Paulo Tradição Hipermercados                         Customer
(19 row(s) affected)

LINQ uses similar syntax and the Union/union operator to generate the preceding output. Here's the C# code:

StringBuilder sbQuery = new StringBuilder();

var query = (
from c in dcNwind.Customers
join s in dcNwind.Suppliers on c.City equals s.City
select new { c.City, c.CompanyName, Type = "Customer" })
from c in dcNwind.Customers
join s in dcNwind.Suppliers on c.City equals s.City
select new { s.City, s.CompanyName, Type = "Supplier" });

foreach (var u in query)
    sbQuery.Append(u.City + "\t" + u.CompanyName + "\t" +
        u.Type + System.Environment.NewLine);
// Write to the text box
txtResult.Text = sbQuery.ToString();

However, the preceding code returns Customer to all rows, instead of the five Supplier instances in the T-SQL query resultset.

According to Matt Warren in a response to my forum post this bug will be fixed in Beta 2.

I would have posted a VB version of the preceding code, but VB won't support the Join keyword until Beta 2.

Missing VB LINQ Keywords

Amanda says in this Where is gone the Group By operator in VB? and and Let Keyword for Visual Basic posts that the following LINQ keywords will be added to VB 9.0 in Beta 2:

  • Join
  • Group By
  • Group Join
  • Skip [While]
  • Take [While]
  • Aggregates (Sum, Min, Max, etc.)
  • Let

as well as these language features:

  • Nullable shortcut syntax (?)
  • Lambda Expressions
  • Partial Methods
  • Ternary If operator*

* Update 6/15/2007: Jim Wooley reported in a comment that a ternary If operator (similar in function to IIf or inline If) will be added per Paul Vick's IIF becomes If, and a true ternary operator post of 5/8/2007.

LinqDataSource Control for ASP.NET

Beta 2 should deliver ASP.NET's LinqDataSource control that Anders Hejlsberg demonstrated at MIX07.

Problems with Stored Procedure Updates in the O/R Designer

This long LINQ to SQL: Problem with Use of Stored Procedures for Insert, Update, and Delete thread in the LINQ Project General forum lists four issues that affect replacing dynamic SQL with stored procedures for table updates in the O/R Designer:

  1. Code generation adds an underscore to reserved words, such as Order in VB which is missing in method calls.
  2. Delete funcitons have an extraneous current parameter.
  3. Update functions have current and original parameter values reversed (discovered by Jim Wooley and Mike Taulty).
  4. To date, I've been unable to match INSERT sprocs with the required signature for tables with identity PKs.

Presumably, these problems will be fixed in Beta 2. Matt Warren says early in the thread:

The problems with the designer code-gen have been resolved and should appear in the beta2 CTP or possibly sooner.

However, I haven't received any direct assurance that the specific problems in items 2 through 4 of the preceding list will be addressed. The party line is: "Databinding will be greatly improved in Beta 2."

I'll add more new features and improvements coming in Orcas Beta 2 to this list as I learn about and/or confirm them.


Anonymous said...

Thanks for summing up the updates we can expect for beta 2.

As you already know, I've been benchmarking the performance of LINQ to SQL and am eagerly awaiting improvements in future releases (Matt Warren, MS, stated that they are aware of the performance and have internal benchmarks that are 15% slower compared to raw SqlDataReader scenarios - again, those are specific situations).

Any new information on this topic?

It's fairly expensive to new up a DataContext and I'm hoping they're providing better performance in terms of entity materialization too.

Thanks in advance
Anders Borum / SphereWorks

Roger Jennings (--rj) said...


I've seen the same statement from Matt, but nothing more concrete. I certainly don't believe that LINQ to SQL will ever be faster than an SqlDataReader. As I understand the architecture, that's providing the transport.


Anonymous said...

I never asked for LINQ to SQL to be faster than native SqlDataReader operations - only asked whether somebody could provide some concrete information on the expected performance of the final product.

Anders Borum / SphereWorks

Anonymous said...

One more VB language feature coming in Beta 2: Ternary IF operator. See Paul's post at http://www.panopticoncentral.net/archive/2007/05/08/20433.aspx