Showing posts with label Lambdas. Show all posts
Showing posts with label Lambdas. Show all posts

Saturday, September 08, 2007

LINQ and Entity Framework Posts for 9/7/2008+

Dinesh Kulkarni Posts Changes to LINQ to SQL RTM

I missed these two important posts because Dinesh posted them to the LINQ Project General forum instead of his Dinesh's Cyberstation blog:

LINQ to SQL Beta2 to RTM Key Changes (August 27, 2007)

  • Table<TEntity>'s Add[All]()/Remove[All]() method names will change to Insert[All]OnSubmit()/Delete[All]OnSubmit(). In my opinion the new names apply relational metaphors to objects, which is a bad practice.
  • The OnValidate() event gets a ChangeAction Enum parameter with None, Delete, Insert, and Update members. Relational terminology again applied to objects.
  • Local method calls in LINQ to SQL projections from VB LINQ to XML queries are now supported. Dinesh provides a LINQ to XML query example.

Beta2 to RTM Changes in Attach() Behavior (September 11, 2007)

  • Attempts to Attach() an object that is not truly detached from another DataContext by serialization/deserialization will throw an exception. An example is an object that has deferred loaders from another DataContext.
  • Objects Attach()ed with SubmitChanges() don't have ChangeSet entities will default to "unchanged" status and no longer will be inserted to the underlying data store by default.

Problems Using Stored Procedures for LINQ to SQL Data Retrieval with ASP.NET

The final version of my first post about Problems Using Stored Procedures for LINQ to SQL Data Retrieval with ASP.NET 3.5's LinqDataSource and GridView controls is done. The primary issues with the LinqDataSoruce are:

  • Lack of Persistence Ignorance (in general, not a result of using stored procs for data retrieval)
  • Loss of server-side paging and consequent increased load on the database
  • Inability to lazy-load EntityRefs without spewing dynamic SQL queries

Subsequent posts will cover issues with BindingSource components and DataGridView controls when using stored procedures to hydrate LINQ to SQL entities. (Update 9/12/2007.)

Frans Bouma Starts LINQ to LLBLGen Pro Series

Frans Bouma, the developer of the LLBLGen Pro commercial O/R mapping tool and principal antagonist of LINQ to SQL and the Entity Framework, has started a project to adapt LINQ to LLBLGen Pro (or vice versa). His first posts are here:

  1. Developing Linq to LLBLGen Pro, Day 0 (September 11, 2007)
  2. Developing Linq to LLBLGen Pro, Day 1 (September 12, 2007)

Thomas Wagner likens Frans' new series to A diary of LINQ pain.

Be sure to read the comments to the first post! (Update 9/13/2007.)

Jim Ng on Lambda Expressions and Expression Trees in VB

Jim Ng, the dev lead for the VB compiler team, promises a series of VB Team posts on lambda expressions and expression trees. The first post of the series is, appropriately enough, Lambda Expressions and Expression Trees of September 11, 2007. He's also written "Lambda Expressions," a "Basic Instincts" column for the September 2007 issue of MSDN Magazine. (Updated 9/12/2007.)

Julie Lerman Explains Why Entities Are Missing Foreign Key Values 

Julie's September 11, 2007 Entity Data Model Associations: Where's my Foreign Key? explains why entity properties don't include foreign keys in the Entity Data Model (EDM) unless they're also part of another entity's primary key. They've been sacrificed in favor of association and navigation properties. For example, the Order entity that's persisted to the Northwind Orders table has no CustomerID, EmployeeID, or ShipperID property; instead it has many:1 navigation properties to associations with Customer, Employee and Shipper entities. (I've singularized the entity names in Julie's EDM diagram.)

The Order_Detail entity's ProductID property, which is a member of the composite primary key, was missing in previous CTPs due to the ADO.NET team's over-zealous pursuit of object purity. Of course, it could have been worse: they could have hidden the OrderID property, too. As Julie points out, relationally-oriented folks like Jay Traband of IdeaBlade and me have complained about the missing foreign keys here and here. Lets hope that the ADO.NET team delivers on their intention to surface foreign key values as an option in the release version. (Updated 9/11/2007.)

Don Demsack (DonXML) Talks LINQ to XML on .NET Rocks Show #271

Don Demsack (a.k.a. DonXML) discusses LINQ to XML and the new XML language features in Visual Basic with Carl Franklin and Richard Campbell in this 66-minute .NET Rocks! audio clip posted September 11, 2007: Don Demsak on LINQ to XML.

ADO.NET Team Wants Entity Framework Docs Feedback

Glenn Gailey of the Entity Framework User Education (UE) team posted Please Give Feedback on Entity Framework Docs on September 11, 2007 requesting feedback :

  • When a topic is particularly effective or ineffective.
  • When the topic doesn't reflect the product behavior (i.e. there's a doc bug).
  • When an important use case or scenario is not covered or more documentation or examples are needed.

Glenn includes instruction on how to submit feedback from the online docs at ADO.NET Entity Framework Beta 2 or the VS 2008 UI.

I'm planning to gripe about the lack of information on the topic of retrieving data by stored procedures. I'm sure Julie Lerman will make the same complaint.

Installing Project Jasper After Entity Framework Tools Clobbers Them

Running the installer that you download from the Jasper : MIX 2007 CTP page after installing the EF Tools August 2007 CTP causes VS 2008 to be unable to load the EDM Designer bits (Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage, Microsoft.Data.Entity.Design.Package, Version 1.0.0.0 ... .)

To regain proper loading of the EDM Designer, uninstall the Microsoft Project Jasper May 2007 CTP, uninstall the ADO.NET Entity Framework Tools and reinstall the Tools.

Update 9/12/2007: Reinstalling the tools didn't solve the problem. I've posted a question about this problem in the ADO.NET Orcas forum.

Update 9/13/2007: Microsoft's Jeff Derstadt said in answer to my question:

The Jasper CTP is not supported for VS 2008 Beta 2. The installer included a patch for an earlier VS 2008 CTP where the EDM tools would not work properly. You will likely need to reinstall VS 2008 Beta 2 to remove this patch.

Andy Conrad in a reply to my comment asking if he had tried it said:

Just to be clear - the sample works with Jasper running over Beta 1 of Orcas.  The problem exists when installing Jasper over Entity Framework Beta 2.  We are currently investigating a solution for this.

I wonder how many folks are running Orcas Beta 1.

(I found this out the hard way when attempting to try the Cappuccino Project Jasper sample by Tok Wee Hyong of the .Net Singapore user group that Andrew Conrad recommended. I wasn't able to get it to run on either of my systems running VS 2008 Beta 2 due to reference version conflicts and lack of the multi-targeting selection button on the project Open dialog. Subsequently, I determined that installation of the Jasper CTP ate my VS 2008 Beta 2 installation.)

John Stallo Explains Building N-Tier Apps in VS 2008 with VB

This Channel 9 Interview - John Stallo on Building N-Tier Applications in VS 2008 (Beth Massi) post of September 10, 2007 from the VB Team also has a pointer to a walkthrough with a strongly typed DataSet noted my LINQ and Entity Framework Updates for 8/29/2007+ post. As mentioned in that post, the topic is apropos because you can substitute LINQ to SQL for the DataSet to evaluate if LINQ to SQL is right for you in a WCF-based service environment.

Mike Taulty Continues to Explore LINQ to XML

His first Monday post XML, LINQ, Databinding a ListBox to an Anonymous Type does exactly what it says, but you can expect to hear more on this topic. Northwind & Photos - How Many Times Will I Write This Code? is a scratch pad for code to write the Northwind.Employees.Photo values to a file instead of encoding them as base64 to a CDATA element. (Updated 9/10/2007.)

Pablo Castro Requests Feedback on Astoria Data Payload Formats

In Sunday's Astoria Design: payload formats Project Astoria Team post, Pablo Castro asks for input on the team's proposed three data payload formats:

  • ATOM/APP (Atom Publishing Protocol) because it's popular: "The important characteristic about ATOM/APP is that everybody speaks it, and it’s increasingly becoming kind of a lingua franca of the web when it comes to data exchange between unrelated agents."
  • JSON (JavaScript Object Notation) also because it's popular: "[JSON] has become popular lately mostly (my guess) because it makes it easy to consume results from services in JavaScript environments, particularly inside web browsers."
  • Web3S (Web Structured, Schema’d & Searchable) as a replacement for POX (Plain Old XML) because "folks at the Live organization in Microsoft were working on a protocol for data exchange that would be used uniformly across many different services."

RDF (Resource Description Format), which was included in the May 2007 CTP has been dropped because "we didn’t see any early user actually using it and we haven’t seen a particular popular scenario where RDF was a must-have."

Without giving it a lot of thought so far, I think I prefer POX to Web3S. (Updated 9/10/2007.)

Erik Meijer Discusses LINQ in .NET Rocks Show #270

Erik Meijer, LINQ's chief spokesman on the conference circuit, joins Carl Franklin and Richard Campbell in Show #270, Erik Meijer on LINQ!!, to deliver 66 minutes of "detail about LINQ from soup to nuts. This is a very technical discussion, and not an overview." Erik starts at 06:30 and covers LINQ to XML, LINQ to SQL's DataContext object, SQL Server versus CLR data types. He discusses Standard Query Operator lambda expressions and their relationship to delegates and monads starting at 43:00, and LINQ's limitations at 52:00. At 53:45, Erik recommends using LINQ to Objects for queries across domain-specific implementations. He explains the SelectMany operator at 56:30, instance-based data updating at 58:45, LINQ to Entities at 1:00:30, and third-party LINQ providers at 1:01:30. (Updated 9/9/2008.)

Julie Lerman Hydrates EF Entities with Stored Procedures

If you think using stored procedures to hydrate (and cripple) LINQ to SQL with ISingleResultset<T> collections is a pain, wait until you try doing the same with Entity Framework entities. As Julie mentioned in her September 8, 2007 Finishing up READ Stored Procs brought in by the Entity Framework EDM Wizard post, selecting a stored procedure node in the EDM Wizard adds a function stub to the EDMX file's <edmx:StorageModels .../> section but doesn't add any elements to the <edmx:ConceptualModels .../> or <edmx:Mapping .../> sections. And there's no documentation for using stored procs to retrieve entity data except a brief answer by Zlatko Michailov on June 9, 2007 to Julie's Sprocs and views in June CTP forum question.

Julie uses Northwind's CustOrderHist stored proc as and example to provide the key to what sections you can generate from the designer and those you must write yourself. She promises that an article in her DevLife blog with step-by-step instructions will be posted shortly. She'll update her personal blog post and I'll update this item when the article's posted.

Mike Taulty Saves Memory by Streaming Documents with LINQ to XML

LINQ to XML's DOM programming model is somewhat more memory-efficient than conventional XML DOM processing, but public streaming input and output APIs would enable processing very large documents without consuming an excessive (usually about 4X the document size) amount of memory by a DOM tree.

Unfortunately, the LINQ to XML team decided in March 2007 to pull the streaming input API from the VS 2008 RTM. However, they left the XStreamingElement type in to enable implementing custom streaming input scenarios. XStreamingElement, to quote the XML Team in their March 5, 2007 Streaming with LINQ to XML - Part 1 post, "allows XML-like trees of IEnumerable<T> instances that can be lazily consumed while being saved as XML text."

Mike demonstrates in his his September 8, 2007 LINQ to XML and Larger Files post that pull-parsing the input document with an XmlReader in a "custom axis" function and a C# iterator reduces memory consumption from 190 MB (for the DOM) to 2 MB for a 100 MB XML file. With conventional output to a file, memory consumption increases to 7 MB. However, substituting XStreamingElement for XElement reduces memory usage to 428 KB.

For VB programmers, Mike provides an XElementEnumerable class to replace C#'s iterator, which VB 9.0 doesn't support.

Scott Guthrie Demos Custom Queries with the LinqDataSource

Scott's LINQ to SQL: Part 9 - Using a Custom LINQ Expression with the <asp:LinqDatasource> control post of late Friday night shows you how to substitute custom LINQ query expressions for the single-table queries that you create with the LinqDataSource's Configure Data Source wizard. The wizard limits customization to selecting all or specific properties of a LINQ to SQL entity to retrieve and specifying a Where clause filter and OrderBy sort of a single table. If you don't retrieve all fields, bound controls aren't updatable.

You can substitute a custom LINQ query expression for the default expression that the wizard generates in a handler for the the LinqDataSource's Selecting event. Just pass the query result's IEnumerable<T> to the e.Result property. Inline editing in DataGrids or other bound controls won't work if you pass a custom data shape or projection.

Update 8/12/2007: Be wary of returning List<T> objects from stored procedures (see Problems Using Stored Procedures for LINQ to SQL Data Retrieval, currently a work in progress.)

Andrew Conrad Posts Generic CopyToDataTable Extension Method

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. Andrew provides the following usage notes:

  • The initial schema of the DataTable is based on schema of the type T. All public property and fields are turned into DataColumns.
  • If the source sequence contains a sub-type of T, the table is automatically expanded for any addition[al] public properties or fields.
  • If you want to provide a existing table, that is fine as long as the schema is consistent with the schema of the type T. 

Andrew's sample source sequence consists of a base class Item, which has public Id, Price and Genre properties. Item has two subclasses: Book has a public Author property and Item has a public Director property. The DataTable generated from the anonymous type has five columns: Id, Price, Genre, Director and Author.

Tip: The sample code requires a reference to System.Data.DataSetExtensions.dll and using System.Data; using System.Data.DataSetExtensions; and using System.Reflection; declarations.

For a brief review of databinding with LINQ to DataSet, see Erick Thompson's LINQ to DataSet – Data Binding post of August 24, 2007. Daniel Moth has a July 27, 2007 LINQ to DataSet item with details of the classes in the System.Data.DataSetExtensions namespace, and here are links to three introductory LINQ to DataSet posts from February 2007: Part 1, Part 2 and Part 3.

Update 9/12/2007: Andrew says in a comment that he intends to update the code to solve these two issues:

1)  The code is not catching the error case when the type T of the source sequence does not match the schema of the provided datatable.  I suppose I could extend the table automatically in this case.

2)  The results of your query is just a sequence of scaler values.  The code wasn't really designed for this and I am not sure I see much value, but I suppose I could just make a table with a single column.

Upgrade List<T> Collections to BindingListView<T> for Sorting and Filtering

My September 7, 2007 Support Sorting and Filtering for List<T> Collections post shows you how to take advantage of Andrew Davey's BindingViewList class to bring advanced sorting and filtering features to controls and components bound to List<T> collections created by invoking LINQ Query's IEnumerable<T>.ToList() method.

Andrew Davey Generates Assertions with LINQ Expression Trees

Andrew Davey's September 7, 2007 Assertions via Linq Expressions post suggests minimizing the boredom of duplicating the code you're testing in a text message string, such as Debug.Assert(input != null, "input != null");, when writing Debug or NUnit assertions. His approach is to compile and invoke LINQ-style expression trees to specify the assertions and then invoke ToString() to write the string.

For example, calling this function

void Assert<T>(T obj, Expression<Func<T, bool>> test)
{
  System.Diagnostics.Debug.Assert(test.Compile().Invoke(obj),
      test.ToString());
}

with

Assert(input, i => i != null);

is the equivalent of writing

Debug.Assert(input != null, "input != null");

Andrew also suggests extension methods to provide alternative syntaxes.

Note: Andrew is the Bristol (UK) developer who wrote the BindingListView wrapper to make List<T> collections sortable and filterable (see Support Sorting and Filtering for List<T> Collections.)

Tuesday, August 14, 2007

Erik Meijer to Demo Volta for the First Time at OOPSLA 2007

Erik Meijer, the "Creator of LINQ," will make four—count 'em, four!—presentations to the ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages and Applications (OOPSLA) 2007 conference in MontrĂ©al, Canada, from October 21 through 24, 2007.

Democratizing the Cloud with Volta (Demo)

Volta is a Tesla incubator project that's charged with development of LINQ 2.0. According to Erik, "We've made very good progress in turning ideas into working code. In particular, the declarative tier splitting is really cool."

This is the first scheduled public demo of Volta's tool-assisted approach to dividing client/server applications into n-tier projects automatically. Here's the latest abstract from Erik's updated paper:

Programming distributed data-intensive web and mobile applications is gratuitously hard. As the world is moving more and more towards the software as services model, we have to come up with practical solutions to build distributed systems that are approachable for normal programmers. Just like Visual Basic democratized programming Windows by removing much of the boilerplate, such as message pumps and window handles, that contributed more to the problem than to the solution, we propose a toolkit of language extensions, APIs, and tools that do the same for web programming. As a result, ordinary programmers can concentrate on the essential aspects of building distributed and mobile applications such as partitioning and flowing code and data across tiers, deployment, security, etc. without getting bogged down in low level details.

Here's a link to Erik's earlier "LINQ 2.0: Democratizing the Cloud" paper, as well as Mary Jo Foley's August 8 2007 A (Microsoft) Code Name a Day: Volta and April 23, 2007 Volta: Microsoft’s dev platform in the Cloud? posts. Here's my original post March 16, 2007 "LINQ 2.0" in Early Returns from QCon 2007, London and subsequent May 18, 2007 Erik Meijer and LINQ 2.0 - Round 2 and August 2, 2007 QCon 2007 and Erik Meijer Coming to San Francisco November 7 - 9 items.

Confessions of a Used Computer Language Salesman (Essay)

Erik wrote "Confessions of a Used Programming Language Salesman: Getting the Masses Hooked on Haskell" for The 11th ACM SIGPLAN International Conference on Functional Programming (ICFP 2006), held in Portland, Oregon on September 18-20, 2006, but his PDF file doesn't carry the ICFP copyright. Here's the OOPSLA abstract:

Programmers in the real world wrestle every day to overcome the impedance mismatch between relational data, objects, and XML. For the past ten years we have been working on solving this problem by applying principles from functional programming, in particular monads and comprehensions. By viewing data as monads and formulating queries as comprehensions, it becomes possible to unify the three data models and their corresponding programming languages instead of considering each as a separate special case. To bring these theoretical ideas within the reach of mainstream programmers, we have worked tirelessly on transferring functional programming technology from pure Haskell, via Comega to the upcoming versions of C# 3.0 and Visual Basic 9 and the LINQ framework. Functional programming has finally reached the masses, except that it is called Visual Basic instead of Lisp, ML, or Haskell!

I first wrote about this paper in my May 16, 2006 Erik Meijer Promotes LINQ at Expo-C 2006 post, but this might be the paper's first formal presentation.

Lost in translation: Formalizing proposed extensions to C# (Paper)

Erik's giving this paper in conjunction with Gavin Bierman (Microsoft Research Cambridge) and Mads Torgersen. Here's the OOPSLA abstract:

Current real-world software applications typically involve heavy use of relational and XML data and their query languages. Unfortunately object-oriented languages and database query languages are based on different semantic foundations and optimization strategies. The resulting "ROX impedance mismatch" currently makes life very difficult for developers. Microsoft Corporation is currently developing extensions to the .NET framework to facilitate easier processing of non-object-oriented data models. Part of this project (known as "LINQ") includes various extensions to the .NET languages to leverage this support. In this paper we consider the current proposals for C# 3.0, the next version of the C# programming language. We give both an informal introduction to the new language features, and also a precise formal account by defining a translation from C# 3.0 to C# 2.0. This translation also demonstrates how these language extensions do not require any changes to the underlying CLR.

Apparently, Gavin's still at work on the paper, but here's a link to an earlier related presentation, "Formalizing and extending C] type inference (Work in progress)" that Gavin presented at the 2007 International Workshop on Foundations and Developments of Object-Oriented Languages (FOOL/WOOD '07).

LINQ from the Source (Demo)

Erik and Ted Neward (Ted Neward and Associates) will join in delivering this 3.5-hour demo that Erik says will be an "introduction to the various LINQ parts with lots of code samples and many questions from the audience. It's great to see that LINQ is catching on!" Here's the OOPSLA abstract:

Language Integrated Query (LINQ) is one of the hot technologies in the next version of Visual Studio. In this session, hosted by one of the designers of VB9, C# 3.0, LINQ to Objects, LINQ to XML, and LINQ to SQL, we will drill down into all the gory technical details behind LINQ, as well as its practical applicability to modern development projects, from large-scale enterprise systems to "tiny webapps on a big database" projects. This is a unique opportunity to get a first hand account of this new technology, the design process, and historical context.

Ted Neward is the author of the infamous "Object/Relational Mapping is the VietNam of Computer Science" essay.

Update 8/15/2007: Unrelated links have moved to LINQ Updates for 8/15/2007.

Monday, July 09, 2007

Orcas and Entity Framework June 2007 CTPs - New Features

Despite Microsoft's deafening silence (so far) about the availability of the Orcas June 2007 CTP, a few pilgrims are spelunking the VM for new features. Folks are using Danny Simmons' new features list to test-drive the Entity Framework June 2007 CTP under the original Visual Web Developer Express (VWDX) June 2007 CTP as well as the full Orcas June 2007 CTP.

Update 7/10/2007: Here's the apparent explanation for Microsoft's deafening silence. The original Microsoft UK Visual Studio "Orcas" June 2007 CTP post reappeared this morning with the following caveat:

The Product Group have released an newer version of Visual Studio, but they aren't releasing it as a CTP. It is aimed at fixing a couple of problems for some targeted customers. The reality of this isn't tested or up to the quality of a proper CTP. You can still use this download, but it has some problems and it isn't supported for TFS. I would recommend waiting until Beta 2. Sorry for any confusion I have caused. [Spelling corrected.]

If the download page says it's a CTP and the four Readmes say it's a CTP, they released it as a CTP. The post's headline now calls it a "pre-CTP."

Tamir Khason of Microsoft Israel posted Orcas June CTP for limited beta is available on July 4, 2007 with this announcement:

After releasing June CTP of .NET framework 3.5, June CTP bits of Visual Studio 2008 (Team Suite and Visual Web Developer) are also ready for limited beta as Layout or VPC image.

and invited readers to contact him for downloading details.

Visual Basic 9.0 Language Features

VB MVP Bill McCarthy's What's new in June Orcas CTP for VB post lists the following new language features:

  • Lambda functions
  • Nullable types get ? shorthand for Nullable(Of ...) and operator elevation
  • Ternary If() function
  • Join, Group By and other missing LINQ Standard Query Operators

Bill also mentions differences in C# 3.0 and VB 9.0 behavior in his Having fun with "Group-Ease", Change notification with Orcas data model and Keyed Anonymous types in VB9 posts.

VB 9.0 Lambda Functions and LINQ Method Call Syntax Issues

Online help's "Lambda Expressions [VB]" topic provides the following as a code example in the "Lambda Expressions in Queries" subtopic for method call query syntax:

Dim londonCusts As IQueryable(Of Customer) = _
    db.Customers.Where(Function(c) c.City = "London").Select(c)

If you create a new NorthwindDataContext that contains a Customer object and assign it to db, then add the above code to a Sub or Function, it won't compile. The c in the Select(c) selector isn't visible, which results in a "Name 'c' is not declared" error.

The following version with type inference compiles and returns the expected result to an iterator:

Dim londonCusts = _
    db.Customers.Where(Function(ByVal c) c.City = "London") _
                .Select(Function(ByVal c) c)

Notice that the ByVal directive is missing from the help topic's sample; if you don't type it, the compiler adds ByVal for you.

The preceding code seems absurdly verbose compared to the corresponding C# method call syntax:

var londonCusts = db.Customers.Where(c => c.City == "London")
                              .Select(c => c);

The following C# query chains an OrderBy lambda:

var londonCusts = db.Customers.Where(c => c.City == "London")
                              .OrderBy(c => c.ContactName)
                              .Select(c => c);

Here's the corresponding VB version:

Dim londonCusts = _
    db.Customers.Where(Function(ByVal c) c.City = "London") _
                .OrderBy(Function(ByVal c) c.ContactName)
                .Select(Function(ByVal c) c)

The problem with chaining the additional VB lambda expression is that you get the wrong IntelliSense's List Members collection (Equals, GetHashCode, GetType, ReferenceEquals and ToString instead of the Customer object's properties) for the intermediate OrderBy and other chained functions between Where and Select. C# IntelliSense has no such defect.

Join Operator Works But Union Method Is Still Broken

VB 9.0 finally acquired the Join operator, but the following code still returns the literal 'Customer' for every row, not a mix of 'Customer' and 'Supplier' values. I described this problem in my May 15, 2007 Changes Coming to LINQ for SQL post. Here's the VB code with Joins:

Dim sbQuery As New StringBuilder

Dim Query = ( _
From c In dcNwind.Customers _
Join s In dcNwind.Suppliers On c.City Equals s.City _
Select New With {c.City, c.CompanyName, .Type = "Customer"}) _
.Union( _
From c In dcNwind.Customers _
Join s In dcNwind.Suppliers On c.City Equals s.City _
Select New With {s.City, s.CompanyName, .Type = "Supplier"})

' Append list header
sbQuery.Append("City" + vbTab + "Company Name" + vbTab + "Type" + vbCrLf)
' Append query results
For Each u In Query
    sbQuery.Append(u.City + vbTab + u.CompanyName + vbTab + u.Type + vbCrLf)
Next

Matt Warren said on May 15, 2007 in response to my Strange Behavior of LINQ Union Operator message in the LINQ Project General forum, "[T]here is a bug on how we treat literals in a union.  We are fixing it."

I plan to do more testing of Join, Group By and other previously missing LINQ Standard Query Operators and will update this post accordingly.

Update 7/11/2007: Extensive tests of multiple Joins with typed and untyped DataSets indicate that it behaves as expected.

Julie Lerman Explores the Entity Framework's New Feature Set

MVP Julie Lerman provides in her illustrated Entity Framework, Some Before & After tidbits post:

  • Illustrated examples of fixes for the missing foreign key values,
  • Views and stored procedures now appear in the Entity Data Model Wizard
  • New support for stored procedures
  • Replacement of default constructors with Public Shared Function CreateObjectName functions.

Breaking Changes in .NET Fx Libraries (Update: 7/11/2007)

LINQ to SQL: INotifyPropertyChanged and Column properties have changed, which generates 100+ errors in small projects. You'll need to recreate your LINQ to SQL classes with the renamed LINQ to SQL Classes template.

LINQ to SQL: Jim Wooley reports in a comment that the System.Data.Linq.AttributeMappingSource and System.Data.Linq.XmlMappingSource classes have moved to System.Data.Linq.Mapping.AttributeMappingSource and System.Data.Linq.Mapping.XmlMappingSource respectively. [Note: MappingSource changed to AttributeMappingSource and XmlMapping changed to XmlMappingSource, and namespace prefixes added.]

LINQ to DataSet: The System.Data.DataTableExtensions namespace has moved from System.Data.Entities.dll to System.Data.DataSetExtensions.dll so you must change references.

LINQ to XML: Fixing "Could not load type 'System.Linq.Func`2' from assembly 'System.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'." exceptions requires adding a reference to System.Linq.DataSetExtensions.dll. This is probably a temporary expedient for the June 2007 CTP.

Problems with the Orcas June 2007 CTP

Julie reported problems running the Entity Framework June 2007 CTP and I encountered an issue running the extended Northwind database for the C# LINQ to Entity samples under the Orcas June 2007 CTP  in this thread in the ADO.NET Orcas forum.

If you've found and tested new language features (especially that support LINQ), please add a comment with a link to your post.

Tuesday, May 08, 2007

New Series on Closures in Visual Basic 9.0

If the term "lifting the variable x" leaves you scratching your head, read Jared Parsons' multi-part Closures in VB blog series. Jared is a developer on the VB team working with the VB compiler and debugger.

Jared describes lexical closures (usually just closures) as:

[T]he underpinnings for several new features in Visual Basic 9.0.  The[y] are part of the guts of Lambda and Query expressions. ... A closure is a feature which allows users to se[a]mlessly access an environment (locals, parameters and methods) from more than one function. ... Closures are responsible for making the single variable "x" available to [multiple] functions in a process that is referred to as "lifting the variable".

Update: 6/15/2007: Following are links to the first two four members of the series:

  1. Closures in VB Part 1: The Basics (May 2, 2007)
  2. Closures in VB Part 2: Method Calls (May 3, 2007)
  3. Closures in VB Part 3: Scope (May25, 2007)
  4. Closures in VB Part 4: Variable Lifetime (June 15, 2007)

Jared says:

This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use. 

So I'll add links to later series members as Jared posts them.

Update: 5/11/2007: If you'd really like to deep-dive into closures, check out these VB 9.0-based posts from Brian Beckmann:

  • Lambdas, Closures, Currying, and All That. Behind the scenes, I’m working on the lambda-execution mode for the IQ97. [IQ97 is a simulator of the HP97 programmable printing calculator that Brian wrote in a 2006 CTP of VB9.] I’m hoping for a few tweaks to VB and LINQ before I can do this satisfactorily, but I can take the opportunity now—with the current CTP —to illustrate the general technique of defining with lambda and what I hope it might look like in VB some time down the road.
  • Closures without Closures; Currying without Currying. Last installment, we found we could write a factorial function with a function call in the recursive branch, but not a recursive call. This ‘nearly recursive’ style allows us to write factorial without using its name.
  • Rebirth of the Y [Combinator]. Let’s review the bookends of our so-far successful foray into recursion elimination. The general theme has been replacement of recursive calls by self-application of functions adhering to recursive types .
  • A Hint and a Challenge about the search for a non-recursive implementation of the Y Combinator in VB 9.0.

Mike Champion's Brian Beckmann on LINQ underpinnings - Bringing functional programming to "Mort" post links to a Channel9 interview with Brian, a former cosmologist: Brian Beckman: Monads, Monoids, and Mort.

Update 5/13/2007: Mads Torgersen, a Microsoft program manager who's responsible for the design of the C# language, plows the ground in Brian's neighborhood to prove that you can write write a recursive lambda expression, using factorial as an example, and can substitute a named generic recursive method for the Y combinator. Here's the result:

i => new Func<Func<int,int>,Func<int,int>>(fac => x => x == 0 ? 1 : x * fac(x - 1))(new SelfApplicable<Func<Func<Func<int,int>,Func<int,int>>,Func<int,int>>>(y => f => x => f(y(y)(f))(x))(y => f => x => f(y(y)(f))(x))(fac => x => x == 0 ? 1 : x * fac(x - 1)))(i)

The preceding looks to me like a write-only function because it's impossible (for most folks) to read. Brian's VB examples are certainly more readable. (After reviewing this post, Sahil Malik said C# has become too complicated.) Mads has a few other interesting posts relating to C# 3.0 and LINQ in his "Language Designers Workshop" blog. (He isn't a prolific blogger.)

Update 5/11/2007: Amanda also says that VB 9.0 will get the Let keyword in Beta 2 and Beta 2 will deliver full suport of lambdas by VB 9.0.

Update 5/15/2007: Paul Vick also covers closures for VB programmers in his two-part series on lambda expressions and an earlier post:

PS: Amanda Silver has posted the slide deck from her VSLive! Orlando The .NET Language Integrated Query (LINQ) Framework presentation. She promises to update this post with links to sample code in a few days and answer questions related to her session.