Showing posts with label Extension Methods. Show all posts
Showing posts with label Extension Methods. Show all posts

Monday, February 16, 2009

LINQ and Entity Framework Posts for 2/5/2009+

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

Entity Framework and Entity Data Model (EF/EDM)

Danny Simmons explains in his D3: Basic Architecture Decisions post of 2/15/2009 the basic goals of his forthcoming DPMud3 Data Programmability multi-user dungeon game. Danny writes:

The first goal for this exercise is to build a real application not just a collection of samples artificially glued together but at the same time to try out new EF features such as POCO, model-first, mockable-interfaces, the n-tier APIs, T4 for code generation, etc. Another key goal is to use good architectural principles and popular app building patterns which means carefully separating concerns, using Inversion of Control to facilitate mocking and test driven (or at least very carefully tested) development, to create an n-tier architecture with WCF-services and a Silverlight client, and to use a model-view-controller pattern to simplify the creation of multiple views and controllers which share a common model.

Danny’s DPMud post of 2/13/2009 recounts the history of the original multi-user dungeon game that he used as a test harness for Entity Framework v.1 and describes the start of “DPMud3: The Zombies Search for Spock,” v.3 of the game for EF v.2. Danny promises to share the source code as soon as he finishes “working out some details with legal.”

Wriju recommends using EntityClient and eSQL to improve performance in his ADO.NET Entity Framework: Using EntityClient and EntitySQL post of 2/12/2009 and shows you how to handle paging in ADO.NET Entity Framework : Paging EntitySQL of 2/13/2009.

Simon Segal tries to get Specifications, Repositories and (finally) Fetching Strategies bundled into a single approach for the Entity Framework in his The Entity Framework and a unified Lazy and Eager loading framework. post of 2/11/2009. Simon concludes:

So far the results look promising. I will have more to say on this shortly and with a bit of luck will be releasing the code which rounds out the triad of a Repository, dynamic querying capability via Specifications and Fetching Strategies for a unified approach for lazy and eager loading that does NOT require you ever explicitly call the .Includes(), Load() or IsLoaded methods and property on the EntityCollection<T>, EntityReference<T> or ObjectQuery<T> objects.

Muhammad Mosa’s Domain Driven Design & Test Driven Development\Design with Entity Framework, Part 1.a Refactoring & Unit Testing post of 2/11/2009 has the following objective:

My target is to achieve decoupling between EF and all layers above it. This include Repository classes. This might not be of a big benefit to everyone, because my repositories implementation will still dependent on IDataContext. But for me it will help to do TDD more smoothly regardless of my underlying data access layer. Beside it might allow me in future to be able to switch from EF to LINQ to SQL and just implement my IDataContext interface for LINQ to SQL.

Downloadable source code for EF is included.

Julie Lerman announces Amazon is finally listing my book as "in stock" on 2/10/2009.

Grigori Melnik’s Thoughts on Agile Software Engineering and Beyond post of 2/9/2009 asks “DAAB and Entity Framework: possible marriage?” Grigori answers:

We’ve looked at this a little bit internally and we do not see any opportunity or value in this marriage. We might be missing something here. For those, who had asked for this, could you please provide more details about how this integration would work and what values you’d derive from it?

Danny SimmonsAvoiding Query Injection Attacks with the EF post of 2/8/2009 explains how to use ObjectQuery<T>’s builder methods to add filters, sorting and projections to EF queries. The builder fully parameterizes the queries.

See Kristofer Andersson’s Tools - Part 10 - Add-ins - Multiple diagram layouts per model in the Linq-to-SQL designer post of 2/11/2009 below.

LINQ to SQL

See Nathan Duchene’s Storing Values in XML & Using Linq to XML in VB to retrieve them post in the LINQ to Objects, LINQ to XML, et al. section.

David Hayden’s LINQ To SQL Tutorials - Lazy-Loading Properties for Performance post of 2/12/2009 shows you how to take advantage of the DelayLoading property to defer loading of large binary objects, such as images, to improve performance.

See Muhammad Mosa’s Domain Driven Design & Test Driven Development\Design with Entity Framework, Part 1.a Refactoring & Unit Testing post of 2/11/2009 in the Entity Framework and Entity Data Model (EF/EDM) for possible use of his interfaces with LINQ to SQL.

Kristofer Andersson’s Tools - Part 10 - Add-ins - Multiple diagram layouts per model in the Linq-to-SQL designer post of 2/11/2009 describes a new feature for LINQ to SQL mapping with his Huagati DBML/EDMX Tools: Layout View. Layout View enables developers to “‘chunk up’ models in digestable pieces; sometimes referred to as subject areas or functional areas.” Kris notes:

All data modelling tools support this, Visio 2003EA, Erwin, ERStudio, even the ‘diagrams’ feature in SQL Enterprise Manager all allow each model to have multiple diagram layouts/views. …

Unfortunately this feature is missing in both the Linq-to-SQL diagram designer and the Entity Framework designer in Visual Studio 2008. Also unfortunate is that the people responsible for those designers in Microsoft’s data programmability group don’t seem to prioritize that ability. Microsoft’s Sanjay Nagamangalam recently wrote “While we understand your scenario, it does not look like the next version of the designer will support viewing subsets of the EDMX at the moment.” in the comments section of the EFDesign blog as a response when a user asked for the ability to view subsets of a model in the EF designer.

Huagati offers a 30-day free trial of the v.1.62 beta version for LINQ to SQL. A decision is pending on extending this feature to the Entity Framework v.1 and will be influenced by user interest demonstrated during the beta.

Shefali Kulkarni takes on Single Table Inheritance using LINQ [to SQL] on 2/11/2009 and describes mapping a single table to a class hierarchy with LINQ to SQL.

Keven Kubasik’s Using Linq to Sqlite Providers: Updated post of 2/9/2009 provides step-by-step instructions for using the EF-enabled Sqlite provider.

Matthieu Mezil writes in his EDM Designer V0.9 post of 2/9/2009 that My first EDM Designer version was a viewer. It's now a real designer. Check it out.

Matthieu Mezil shows you how to Integrate cache in the query with the IntegrateContext(context, where.Compile()) method (2/6/2009) in a LINQ to Entities query.

LINQ to Objects, LINQ to XML, et al.

Nathan Duchene’s Storing Values in XML & Using Linq to XML in VB to retrieve them post of 2/13/2009 describes a project on which he was working:

Take uploaded XML files, do some file validation against a database using LINQ to SQL, and distribute these files across our network based on the values within the XML file.  Through all this, wire in notifications if the file fails a validation test.

The VB LINQ to XML implementation described in Nate’s post shows you how he accomplished the feat.

Bart De Smet hollers Help! Drowning in Expression Trees, What Now? on 2/12/2009 when attempting complex expressions that contain user-defined functions in LINQ to Objects predicates.

Suprotim Agarwal shows you how to Group By Multiple Values in LINQ in his post of 2/12/2009.

Matthieu Mezil wants to write an extension method Sum on IEnumerable<int> and an extension method Sum on IEnumerable<decimal> and then factorize this code in his Code factorization with delegate post of 2/11/2009. Matthieu asks “how to factorize the initialization (= 0 with an int and = 0 with a decimal) and the addition?” and then shows you the code.

LinqMaster’s 7 LINQ Tricks to Simplify Your Programs post of 2/9/2009 shows you how to:

    1. Initialize an array
    2. Iterate over multiple arrays in a single loop
    3. Generate a random sequence
    4. Generate a string
    5. Convert sequences or collections
    6. Convert a value to a sequence of length 1
    7. Iterate over all subsets of a sequence

Kai Jaeger’s JSINQ - LINQ to Objects for JavaScript CodePlex project of 2/1/2009 is a JavaScript library that allows you to write SQL-like queries against arrays and DOM node lists. According to Kai:

JSINQ is a complete implementation of LINQ to Objects in JavaScript. What that means is that if you know LINQ and you know JavaScript, you know JSINQ. JSINQ is both an API-compatible implementation of System.Linq.Enumerable and a complete query-expression compiler. That's right: you can write LINQ-style queries in JavaScript. And if that isn't enough: JSINQ is also very liberally licensed, well-document, reasonably well-tested (the Enumerable-part) and currently in beta. So give it a go!

Bart de Smet takes up matrix methods in his LINQ and The Matrix – Introducing MLinq post of 10/11/2009. MLinq provides “a very simple way to perform symbolic matrix calculations based on LINQ expression trees.” For consistency, I prefer the name “LINQ to Matrices”

Fabrice Marguerie’s Converting LINQ queries from query syntax to method/operator syntax post of 2/6/2009 describes how to “convert a LINQ query expression (query syntax) to a query operator call chain (method syntax or dot notation).” The solution definitely wasn’t intuitive.

ADO.NET Data Services (Astoria)

Steve Naughton’s back with an AJAX History in a Dynamic Data Website post of 2/13/2009 that explains how to improve users’ experience when navigating history with several filters on each list page.

Rob Bagby explains working with AtomPub in Exposing ATOM feeds from your services – part II – customizing and extending the ATOM feed of 2/13/2008 and the importance of RESTful interfaces in Exposing ATOM feeds from your services of 2/12/2009. Astoria and Azure services implement RESTful interfaces.

Shawn Wildermuth’s Twin Cities Developers' Guild Code Sample of 2/13/2009 includes the source code for a data application using Silverlight and ADO.NET Data Services to show how Line of Business apps can work in Silverlight.

Beth Massi is up to part 3 of her Astoria-powered Office Business Application series in OBA Part 3 - Storing and Reading Data in Word Documents of 2/12/2009. Earlier episodes are

Steven RobbinsSelf Hosting ADO.Net Data Services (Astoria) – Part II – A Basic WCF Service Example follows up his original article with a very basic introduction to WCF services in general using Astoria as the example. Source code is available for download.

ASP.NET Dynamic Data (DD)

Jonathan Carter’s Dynamic Data: Putting A New Dress On An Old Model post of 2/12/2009 starts with this observation:

Within the .NET 3.5 SP1 release, the initial consumer of Dynamic Data (and the newly introduced data annotations) was ASP.NET WebForms. This means that if you’re developing a data-driven web application, and your weapon of choice is WebForms, then you just happen to be in luck. Work is already underway to introduce some of Dynamic Data’s behavior into ASP.NET MVC that is appropriate to its style of development, and you can expect to see additional UI frameworks taking advantage of its semantics and data annotations as well in the near future.

And then asks and answers: “How can I begin taking advantage of it within a WebForms application?”

SQL Data Services (SDS) and Cloud Computing

This topic moved on 1/3/2009 to Windows Azure and Cloud Computing Posts for MM/DD/YYYY+.

SQL Server Compact (SSCE) 3.5 and Sync Services

Liam Cavanaugh’s Customer Spotlight - Synchronization Scalability and Live Toolbar post of 2/11/2009 discusses the scalability of syncing Website Favorites across multiple PCs with the new version of the Live Toolbar and the Microsoft Sync Framework.

Liam’s Customer Spotlight – Interscape Flip for the Mobile Sales Workforce of 2/10/2009 introduces another company, Interscape, who has developed a simple to use sales tool called FLIP Client. Liam writes:

Flip was built to incorporate features like scheduling, pipeline reporting, outlook and more.  I had a chance to talk with Matt Francis, CEO of Interscape and this is what he had to say about the Sync Framework and Occasionally Connected Applications.

Mary Jo Foley’s In Apple’s footsteps: Google licenses Microsoft ActiveSync post of 2/9/2009 describes how:

Google apparently is licensing ActiveSync in order to allow tighter synchronization between Exchange and its  newly unveiled Google Sync service.

Miscellaneous (WPF, WCF, MVC, Silverlight, etc.)

 

Monday, December 22, 2008

“VS 2008 Kicks Off” and “The Road Ahead” make Visual Studio Magazine’s Top 5 Articles for 2008

My “Visual Studio 2008 Kicks Off” cover story for the January 2008 issue of Visual Studio Magazine hit second place in the Top 5 Articles for 2008 and “The Road Ahead” in the same issue made #4, according to Redmond Media Group’s .NET Insight newsletter, Vol. 9, No. 75 of December 18, 2008.

Both stories covered Microsoft’s release to manufacturing (RTM) of Visual Studio 2008 and .NET Framework 3.5 on November 19, 2007. “VS 2008: The MIA Features,” my third story in that issue, described technologies that didn’t make the RTM cut. VS 2008 SP1 and .NET 3.5 SP1 included many formely MIA features.

Following are decks for the three stories:

Visual Studio 2008 Kicks Off: Visual Studio 2008 is all about integrating new .NET Framework 3.x Web and smart-client technology, increasing developer productivity, and managing the application lifecycle. Find out how VS 2008's greatly expanded feature set and new .NET Fx 3.5 namespaces can boost your career as a professional developer and add to your programming enjoyment.

VS 2008: The Road Ahead: Microsoft was already well on its way to creating the next version of Visual Studio (code-named "Hawaii") when it released VS 2008. Learn what the future holds for .NET developers.

VS 2008: The MIA Features: Visual Studio 2008 is an ambitious new release and it includes a slew of new language features and tools that were required to get LINQ up and running by itself and with SQL Server 200x. Of course, not everything planned made it into the product. Here's a description of the elements that were pared back and what their status is now.

The other three stories in the top five were:

#1: Create a Data-Driven Messaging System by Paul D. Sheriff (October 2008)
Hard-coded messages don't scale well, and .NET's inherent error messages are user-unfriendly. Learn how to create a more scalable and user-friendly data-driven messaging system.

#3: Ask Kathleen: Understand Your Code Better by Kathleen Dollard (October 2008)
Visual Studio Team System's Code Metrics feature isn't perfect, but if you understand what it's measuring and how, you can use it to gain insight into your apps' overall complexity and to spot potential problem areas.

#5: On VB: Add Distinction to Your Code by Bill McCarthy (November 2008)
Learn how LINQ, extension methods, and lambda functions can help you add a bit of distinction, simplicity, and robustness to your code.

Tuesday, April 29, 2008

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

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

Matt Davey: Investment Banking Needs PLINQ

In his Parallel Extensions to the .NET Framework article of April 23, 2008 for Dr. Dobbs Portal, financial services developer Matt Davey discusses Microsoft Research's Dryad Network and DryadLINQ and suggests using a C# 3.0 extension method to use PLINQ to submit work to a DigiPede Distributed Computing Network. Matt concludes:

Sitting in the financial vertical, PLINQ can't be released quickly enough. As long as banks understand the implications, PLINQ will improve the trading cycle. Microsoft, however, needs to continue to push in the concurrency field, making developers' lives easier by giving them access to cores locally as well as remotely, and ensuring that the development/debugger tools keep up with the library as it advances.

Added: 4/29/2008

Julie Lerman Creates an Extension Method to Visualize EntityStateObjects

Julie's An extension method for visualizing EntityStateObjects post of April 29, 2008 shows the design and provides the source code to simulate a debugger visualizer for the current EntityStateObject. The EntityStateObject isn't serializable, so she wrote an extension method with the DEBUG ContitionalAttribute to prevent it from appearing at runtime.

Bravo, Julie! And thanks for the source code.

Added: 4/29/2008

Rob Conery Implements a Repository+ Pattern by Passing Out an IQueryable<T>

Rob starts a controversy by redefining the Repository pattern to a Repository+, which outputs IQueryable<T>. With help from Oren Eini (Ayende Rahien), Rob adds a LazyList<IQueryable<T>> to provide lazy-loading generic list without relying on LINQ to SQL's built-in lazy loading feature.

He then goes on to discuss with John Galloway the use of HTML and CSS in ASP.NET MVC to create a UI for the project replacing tables with divs for layout.

Added: 4/29/2008

Randolph Cabral Continues His LINQ to SQL n-Tier Series

Exploring N-Tier Architecture with LINQ to SQL (Part 2 of n) of April 29, 2008 proposes wrapping a LINQ to SQL NorthwindDataContext object with a NorthwindBusinessContext to decouple the DataContext from the business layer and adds GetCustomerById() and PersistState() methods to more closely resemble the ActiveRecord pattern.

Added: 4/29/2008

Google Engineer Admits the App Engine Has Latency Problems Reading and Writing Datastore Entities

Perhaps apps running on the Google App Engine aren't as scalable as one might assume. According to Ken Ashcroft, a Google software engineer, who wrote Tips on Writing Scalable Apps of April 29, 2008:

    • Avoid contention on datastore entities. If every request to your app reads or writes a particular entity, latency will increase as your traffic goes up because reads and writes on a given entity are sequential. One example construct you should avoid at all costs is the gobal counter, i.e. an entity that keeps track of a count and is updated or read on every request. There are some interesting ways of simulating this behavior that don't require reads/writes on every request, and we'll talk about a handy way to cache entities for reads below.
    • Avoid large entity groups. Any two entities that share a common ancestor belong to the same entity groups. All writes to an entity group are sequential, so large entity groups can bog down popular apps quickly if there are a lot of writes to that group. Instead, use small, localized groups in your design.
    • Write sparingly. Writes are more expensive than reads; keep this in mind when designing your data model. If you can avoid a write--it's best to do so.

Added: 4/29/2008

Charlie Calvert Seeds the LINQFarm with an IEnumerable<T> Series

C# community evangelist Charlie Calvert starts another multipart LINQFarm series with LINQFarm: Understanding IEnumerable<T>, Part I, which (as you'd expect) covers the IEnumerable<T> interface.

The second member of the series promises to answer "What is it about IEnumerable<T> that makes it a useful data source and return type for LINQ to Objects queries?"

Added: 4/29/2008

Phil Wainewright Translates Steve Gillmor's Live Mesh Accolade

Phil's Gillmor: Why Google should worry about Live Mesh post of April 28, 2008 summarizes Steve's sometimes rambling 1,000+ words from his surprisingly effusive TechCrunch article, Microsoft Says Yes With Mesh of April 27:

Other commentators have dismissed Mesh as simply a mechanism to protect Microsoft’s desktop-bound assets, but Steve turns that around and points out that what Mesh is really about is connecting the desktop into the cloud (Meshing the desktop into the cloud, as I wrote last Thursday).

Call it self-serving if you like, but Microsoft needs a bridge that will carry its existing market presence over into the cloud and Mesh is that bridge. You could equally call it customer-friendly: Microsoft users will likely be pleased to have a mechanism that helps them make that transition without orphaning their desktop and on-premise IT assets.

Robert Scoble has a shorter translation in Mind Meshing with Steve Gillmor of April 27, 2008:

The only good excuse I’ve heard so far why Microsoft Mesh isn’t interesting is “I hate Microsoft.”

That’s a tough thing to overcome, but I thought Steve Gillmor was one of those who hated Microsoft too. After all, he bought a Mac and kept repeating on his blog “Office is dead.”

But, let’s translate Gillmor: Microsoft Mesh is fascinating. Agreed.

Steve didn't sound all that enthusiastic about Live Mesh during the Gillmor Gang's interview podcast with Dave Treadwell that I reported in the "Sync Guru (Rafik Robeal) Questions the Problems Live Mesh Will Solve" section of LINQ and Entity Framework Posts for 4/21/2008+.

Note: If you're wondering why Live Mesh is germane to the usual topics of this blog, it's because it includes a LINQ implementation ("LINQ to Mesh") and of the potential interaction of Live Mesh with ADO.NET Data Services and SQL Server Data Services (SSDS) as a primary data sources. (Updated 4/29/2008)

Update 4/29/2008: Ori Amiga attempts (but fails) to execute a LINQ to Mesh query against Mesh objects at 47:25 into the Ori Amiga: Programming the Mesh Channel 9 video segment of April 24, 2008. Starting at about 50:00, he shows the code for a typical LINQ query and mentions that the Mesh team is working with the Astoria folks to make sure their query and URL syntaxes are the same.

Other Channel9 videos about Live Mesh:

Bart DeSmet Follows Up on His TechDays Sessions on IQueryable

His Q: Is IQueryable the Right Choice for Me? post shows you how to:

[C]reate your own query pattern implementation by means of instance methods ... by providing an entry-point like Table<T> and further providing a fluent interface pattern that produces Query<T> objects in a chained manner.

as an alternative to implementing IQueryable<T>.

It's nice to see Bart returning to posts about LINQ.

Dinesh Kulkarni Has Resumed Posting about LINQ to SQL

After more than six months of silence following his move "I have moved to another project with the developer division at Microsoft (more about that on a sunny day)" reported in his LINQ to SQL: What is NOT in RTM (V1) post of October 15, 2007, Dinesh posted Lifetime of a LINQ to SQL DataContext on April 27, 2008.

Apparently the sun hasn't shone in Redmond since mid-October.

His latest post provides guidelines for answering the question of whether the DataContext should be short- or long-lived.

As I said in my comment: Welcome back!

Eugenio Pace Produces Screencast of LitwareHR Using SSDS

End to end demo of LitwareHR on SSDS provides an 11-minute screencast of LitwareHD using SQL Server Data Services instead of SQL Server 2005 as the back-end datastore. Eugenio divides the screencast into two acts:

    1. Tenant Provisioning and customization (takes the first 5 min approximately). I show the initial tenant provisioning (creation of a new tenant in LitwareHR, initial configuration, etc) and then a basic customization (e.g. look & feel, position entity shape, etc). You'll notice that I switch back and forth between www.litware.com and SSDS to show how entities look like in the store.
    2. Using the new created instance (this takes the last 6 min). I show a hypothetical recruiter logging-in, opening new positions, then an applicant browsing the open positions and submitting a Resume and finally the recruiter browsing the posted applications. Again, you'll see how do these entities look like in SSDS.

Here are Eugenio's introduction and six-part tutorial about moving LitwareHR to SSDS:

Barry Gervin Starts Yet Another Entity Framework Series

His kickoff, The Entity Framework vs. The Data Access Layer (Part 0: Introduction) of April 27, 2008, explores the roles of a data access layer (DAL, not necessarily a tier) and lays out questions related to it's implementation.

Barry promises that in Part I:

I'll explore the idea of the Entity Framework replacing my data access layer and evaluate how this choice rates against the various objectives above. I'll then continue to explore alternative implementations for a DAL using the Entity Framework.

Liam Cavanagh Offers Sync Framework Tutorials

In apparent response to the increased interest in data synchronization spiked by Live Mesh, Liam's New Sync Framework Tutorial of April 28, 2008 offers the following four projects:

  • Sync101 With Metadata Store – Demonstrates a basic provider for an in-memory data store (replica) that uses the SqlCeMetadataStore class to store the metadata in a SQL Server Compact Edition database.

  • Sync101 Refactored – Factors out the replica-specific code from the code common to any provider in preparation for the next step of adding an additional provider. Also uses a more flexible transfer mechanism class to allow accommodation of more complex schemas.

  • Sync101 Add Xml Provider – Adds a new provider that represents a data store comprised of XML files.

  • Sync101 Add Csv Provider – Adds a new provider that represents a data store containing Comma-Separated Values (CSV) files and also uses XML configuration files to specify the transfer mechanism class properties and the property mappings between providers.

You can download the source code from the MSDN CodeGallery.

Julie Lerman Unveils the Forthcoming ASP.NET EntityDataSource Control

Repeated from: LINQ and Entity Framework Posts for 4/21/2008+

Danny Simmons demonstrated the EntityDataSource control at the MVP Summit 2008 and Julie shares her notes in Sneak Peek at the EntityDataSource Control of April 27, 2008. The EntityDataSource control is the Entity Framework's answer to LINQ to SQL's LinqDataSource control for databinding ASP.NET GridView, ListView and other bindable server controls.

Two important EntityDataSource control features that Julie describes are:

Although I don't remember seeing it during the session, Danny did say that you can choose to eager load related data in the same way that you can with the Include method. I don't know how this is done or if it will impact updating, but I don't know why it would.

One of the most interesting things about the EntityDataSource is that along with its' ability to perform server side paging, it also performs client side caching - of current AND original data. The original data is not stored as complete objects, but the minimal data necessary to reconstruct state when it's time to update. Updates happen, like any other data source, one at a time. So you have to pick an item, edit it and update it.

Availability of the EntityDataSource control will enable ASP.NET Dynamic Data scaffolding to use EF as an alternative to LINQ to SQL as its data source. (See David Ebbo's Dynamic Data at MIX, and upcoming changes post of March 6, 2008.

Hopefully, the EntityDataControl won't share the problems with the LinqDataControl that Julie described in her January 22, 2008 Thinking about the EntityDataSource post.

Note: The link to the OakLeaf blog in the latter post goes to the home page. Julie might have intended to link to go to the Problems Using Stored Procedures for LINQ to SQL Data Retrieval (Updated) post of October 2, 2007. That post contains a "Problems with Stored Procedures That Affect ASP.NET Projects Only" section, which covers sorting and paging problems.

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.)

Friday, September 07, 2007

LINQ and Entity Framework Updates for 9/5/2007+

This post is late because of an extended investigation I'm making into use of stored procedures for LINQ to SQL data retrieval. I'm also working on a detailed comparison of the performance of LINQ to SQL queries and Typed DataSets. Stay tuned for the initial results and a more detailed performance analysis of LINQ to SQL queries in an article scheduled for the November 2007 issue of Visual Studio Magazine.

Julie Lerman on Creating DataTables from LINQ to Entities Queries

Julie's Converting a LINQ to Entities query to a DataTable post of 9/7/2007 shows you how to modify Mike Taulty's replacement for LINQ to SQL's now-missing ToDataTable() method in his "Orcas" March CTP - IEnumerable<T> to DataTable post to generate a DataTable from a LINQ to Entities query.

Julie Lerman Profiles LINQ to Entities Queries

In another How do LINQ to Entities queries surface in the database? post today, Julie explores the T-SQL generated by executing queries of varying complexity against the AdventureWorksLT database. Several of the convoluted T-SQL queries create by relatively simple LINQ to Entities queries are interesting (in the sense of the purported Chinese curse: "May you lead an interesting life."

Rob Conery Extends LINQ to XML to Geocoding

Rob Conery, creator of SubSonic, chief architect of the Commerce Starter Kit, and author of How To Use LINQ To Query Just About Anything, has been exploring the use of LINQ to SQL and the Haversine Formula with LINQ to SQL queries that find hotels within a given distance from a particular latitude and longitude. His first attempt led him to calculate the distance with a T-SQL scalar function in his August 30, 2007 LINQ and Geocoding post, because the simple-appearing lambda expression incurred a “Cannot translate this to SQL” from LINQ to SQL. (The query did return the correct result with LINQ to Objects, a.k.a. data in memory.)

Rob's September 6, 2007 LINQ and GeoCode, Part 2 post explains that the secret to making the lambda expression work was to declare it as an Expression<T> so the compiler builds an expression tree out of it. (See the Michael Giagnocavo post below.) Unfortunately, the expression tree approach turned out to be three orders of magnitude slower than the T-SQL function.

Mark Leistner Starting Unit Testing with LINQ to SQL Series

Mark's introductory post, Unit Testing in LINQ - Part 1, of 9/5/2007 says:

So recently I've started using LINQ a lot and it saved me a lot of time and effort once I got over the big hump of rewriting all my objects to take advantage of it. I began to see the potential that I could possibly use this to test my project better than any other method I had tried, and to do it rather easily.

I tried several ways to use LINQ in unit testing, but kept running into problems. Eventually I came up with a solution that so far has been rather easy to implement and provided just what I had been looking for. This is the first post in a series that I am creating to pass this knowledge on, hopefully you will find it useful for your projects as well.

Unit Testing in LINQ - Part 2 (9/7/2007) describes Mark's "base code" to allow unit testing with LINQ to SQL, which uses an IRepository interface having a Context property to access the underlying DataContext and an IRepositoryTable object for each DataTable. Unfortunately, this approach doesn't support EntityRefs and EntitySets.

Unit Testing in LINQ - Part 3 (9/7/2007) includes code for retrieving a simple Customer object by ID, three unit tests, implementation of the Repository, and implementation of the TestTable object.

I'm not sure I'm ready to give up EntityRefs and EntitySets at this point in my unit testing for LINQ search.

Michael Giagnocavo Solves the Haversine Lambda Problem

Michael's Calling custom methods in LINQ-to-SQL and Complicated functions in LINQ to SQL posts of September 5/6, 2007 explains that

[C]alling our [custom] method in our LINQ query won't work because the LINQ-to-SQL code isn't going to know what to do with it. A method is just an opaque block of code with a name.

Instead, you declare an Expression<LambaFunc> and change the Where appropriately, as shown in his first post. He references Thomas Petricek's Building LINQ Queries at Runtime (see below) and Joe Albahari's Dynamically building LINQ expression predicates. (Joe is the author of the great LINQPad application I described in my LINQ and Entity Framework Updates for 8/22/2007 post.)

In the second post that handles the Haversine Formula problem, Michael notes:

If you want to use your own "complicated" functions with LINQ to SQL, you'll need to manually construct the predicate expression. It's not pretty, but, it does let you convert somewhat detailed functions, such as Haversine, inside of LINQ-to-SQL queries. This is not always the right approach: in some cases it'll be better to use a UDF or stored procedure.

and shows the T-SQL output by the expression tree.

Bart de Smet Uses LINQ to XML for PowerShell Extension Methods

Bart's September 6, 2007 Extension Methods in Windows PowerShell post explains how to use LINQ to XML to generate the XML files needed to take advantage of Windows PowerShell's Extended Type System (ETS).

Jim Wooley on Compiling LINQ to SQL Queries

Jim Wooley's LINQ to SQL Compiled Queries of September 5, 2007 provides a more detailed analysis of compiling LINQ to SQL queries in VB 9.0. This is a topic that Rico Mariani introduced in his LINQ to SQL performance posts and that I commented about in Rico Mariani Starts LINQ to SQL Performance Analysis Series and expanded on in Rico Mariani's DLinq Performance Tips (Part 2) and Compiled Queries.

My performance data was based on VS 2008 Beta 1, so I'm planning update my Rico Mariani's DLinq (LINQ to SQL) Performance Tips (Round 3) post with Beta 2 data this weekend.

Cascading Deletions and the "DeleteOnNull" Problem

If you've encountered this error:

Error 9 DBML1055: The DeleteOnNull attribute of the Association element 'ConstraintName' can only be true for singleton association members mapped to non-nullable foreign key columns.

three potential solution to the problem are in my Cascade Deletion Problem with LINQ to SQL Beta 2 post of 9/5/2007.

Mike Taulty Posts Brief LINQ to Entities Queries

Mike's September 5, 2007 ADO.NET Entity Framework - LINQ. Getting Started post compares the ObjectContext.CreateQuery<T> method to create an ObjectQuery<T>, which implements IQueryable<T>, with a similar LINQ to SQL query over the DataContext to return the same IQueryable<T>.

Julie Lerman Explores the EDMDeploy Build Task

Entity Framework (EF) Beta 2 introduces the new EDMX file, which incorporates data previously contained only in the SSDL, MSL, and CSDL files. In her 9/4/2007 Sharing Entity Framwork models between projects (or teams) when developing post, Julie describes how the EDMDeploy build task generates the the SSDL, MSL, and CSDL files from the EDMX file and the files' default location in Windows and Web form projects. She also shows how to edit the EDMDeploy task in the Post-build Event Command Line editor to place the three files in a more convenient folder.

Matt Warren's Provider Implements Joins and SelectMany

Matt's LINQ: Building an IQueryable provider - Part VII of September 4, 2007 explains that LINQ to SQL queries with two From clauses produce a cross-product and a Join clause generates an explicit join; both return the same result. He then goes on to create an expression tree to generate T-SQL CROSS JOIN, INNER JOIN, or CROSS APPLY queries. T-SQL's CROSS APPLY, which is new in SQL Server 2005, solves problems with translating certain types of SelectMany clauses. His post contains a link to the Query7 project's source code.

Bart de Smet Finishes his New VB 9.0 Language Features Series

He added XML Support on 9/4/2007 and LINQ Queries on 9/5/2007: 

If you want a concise guide to VB 9.0's new features, Bart's series is the best source.

Julie Lerman Checks Serialized Entity Keys in ASP.NET ViewState

She notes in her September 3, 2007 EntityKey and ASP.NET ViewState post that entity objects didn't serialize without errors in the Beta 1 Entity Framework release.

Bill Burrows on LINQ to XML with VB 9.0 (Article and Video Clips)

Programming With XML Using Visual Basic 9 by "VB Professor" Bill Burrows explains how to use VB 9 and LINQ to XML to transform Windows Live Expo's RSS feed of homes for sale into an Excel worksheet in Open Office XML format. You'll find 13 brief video clips that describe the entire process here.

Dynamic Searching with LINQ and VB 9.0 Expression Trees

The VB Team's Jonathan Aneja posted Implementing Dynamic Searching Using LINQ, which shows you how to add an InfoPath-like Condition Builder control to a Windows form and write a companion expression tree module to enable dynamic LINQ to SQL searches with property names and operators selectable from dropdown lists. Users enter values into text boxes, datetime pickers or check boxes, depending on the data type. (Posted August 29, 2007 and somehow missed at that time.)

Tomas Petricek Takes Another Approach to Dynamic Queries with C#

Building LINQ Queries at Runtime in C# by Tomas Petricek (a.k.a. Tom.ASP) demonstrates an alternative approach to coding dynamic LINQ to SQL queries: “call functions in LINQ queries.” Here's his introduction:

[Y]ou can call your functions (which are actually expressions) using the Expand extension method. ... The ToQueryable [method] creates a thin layer that processed the entire LINQ query before it is sent to the “LINQ to SQL” translator and it replaces all occurrences of the Expand call with the body of the function, so that the “LINQ to SQL” can understand the query.

A DLL is available for download from the post and source code is on CodePlex (Posted July 30, 2007 and missed).

Friday, July 13, 2007

New LINQ with Visual Basic Articles

The VB Team has lagged the C# group in implementing LINQ-related keywords and language features throughout the VS 2008 beta process. This situation is understandable because the C# group is in charge of the core LINQ specification, so the VB compiler group can't complete their work until the C# implementation is fully baked.

The same has been true for LINQ documentation, but it's less understandable. For example, the May 2006 .NET Standard Query Operators white paper has only C# 3.0 sample code. Until Wednesday, I hadn't seen a single working example of an expression tree or custom LINQ implementation written in VB; they were all C#-based.

LINQ to WDS

The tide may be turning with Kevin Halverson's LINQ to WDS project. (WDS = Windows Desktop Search.) In How to implement IQueryable (Part 1) and How to implement IQueryable (Part 2), Kevin delves into a detailed explanation of how to create a custom LINQ implementation that lets you execute queries against the file system with WDS's OLE DB driver. WDS is part of Vista, but you can download versions for Windows XP and Windows Server 2003 here. The project translates simple LINQ Select and Where clauses into SQL statements like this:

SELECT System.ItemPathDisplay FROM SystemIndex WHERE NOT CONTAINS(System.ItemType, 'folder') AND (System.DateCreated >= 'EarliestDate' AND System.FileName LIKE 'Expression')

Kevin's Part 1 post provides a link to download the complete source code, which totals about 500 lines. The UI is a bit Spartan: A message box wrapped by a For Each loop substitutes for the usual console output.

Note: Kevin says the project requires Beta 2 because IQueryable was been refactored after Beta 1 released. However, it works for me on the Orcas June 2007 CTP.

VB Query [In]Comprehensions

Scott Wisniewski's "Basic Instincts: Query Comprehensions" column in the August 2007 issue of MSDN Magazine covers VB's query translation, lazy evaluation, the Select, SelectMany, Where and Join query operators, and expression trees in much greater depth than I've seen before (other than in Kevin's two posts).

Scott includes a sample project that instruments most of the 48 (or 50 if you count TakeWhile and SkipWhile separately) Standard Query Operators with PerfMon counters. The current version throws an error when you open the solution because of a missing unit Test.vbproj project file. You can fix that error by removing its entry from Microsoft.Linq.Instrumentation.sln.

There's a 45-KB EULA but not even a Readme file to explain how to use the WrappedEnumerable, WrappedEnumerator, and WrappedOrderedCollection classes. I couldn't find a single comment in the project. This makes the sample project incomprehensible to most LINQ neophytes.

The VB Team must have been confident that Beta 2 would be available by the magazine's posting date because Scott's project does require Beta 2. (It needs Beta 2's IOrderedSequence interface and CreateOrderedSequence method to compile; they aren't in the Orcas June 2007 CTP.)

Maybe Scott will add some documetation to his Shameless Plug (Scott Wisniewski) post.

Monday, March 12, 2007

Updated LINQ SampleQueries and New LINQ to XML in VB Video

Charlie Calvert has posted a further update to the March 5, 2007 version of the C# SampleQueries for the VS Orcas March 2007 CTP. The new update, which you can download here or from Charlie's blog, includes about 101 LINQ to Entities queries, which were missing from the earlier update.

According to Charlie, the latest update contains:

  • LINQ to Objects
  • LINQ to SQL
  • LINQ to XML
  • LINQ to XML Part II
  • LINQ to DataSet
  • LINQ to Entities

Andrew Conrad (of ObjectSpaces fame) notes that Erick Thompson posted on March 6, 2007 workarounds for the first set of updates.

Paul Vick provides a heads up for a new Channel9 video about VB 9.0's LINQ to XML features for Orcas. The 12:47 "LINQ to XML API and VB 9.0" segment shows lead developer David Schach's VB 9.0 application that creates a weekly RSS feed for new photographs on his Web site. Hopefully, VB's LINQ for XML API can be used for something other than RSS feeds.

Slightly Off-Topic

Scott Wisniewski, a developer on the VB 9 Compiler Team has posted the sixth a series of articles on the new extension methods included in Visual Basic 9.0's "Orcas" release: Extension Methods Best Practices.

The first installment of Granville Barnett's "Introducing LINQ" series appeared today on dotnetslackers.com. Granville says the article covers:

  • Querying of in-memory collections
  • A subset of the standard query operators
  • Compiler generated code including a discussion on "under the covers" compiler behaviour like cached anonymous delegates and delegates
  • Lambda Expressions