SharePoint: Exception HRESULT 0x80020009 when using SPSiteDataQuery

I experienced the following error in WSS3 when performing a SPSiteDataQuery across lists that do not derive from the base type SPBaseType.DocumentLibrary.

Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION)) at Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns) at Microsoft.SharePoint.SPWeb.GetSiteData(SPSiteDataQuery query) at MyClass.GetDataGridData()

It turns out that the problem occurs when the LinkFileName field is used in the ViewFields property of your SPSiteDataQuery (even if this viewfield is nullable). The following code illustrates a fix to return the all the ViewFields for a cross list aggregation (given a start list to aggregate from).

private string GetViewFieldsXml(string webSiteUrl, string listName)
{
    using (SPSite site = new SPSite(webSiteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList sourceList = web.Lists[listName];
            StringBuilder viewFields = new StringBuilder();
            // Get view fields
            foreach (SPField currentField in sourceList.Fields)
            {
                if (sourceList.BaseType == SPBaseType.DocumentLibrary
                    || currentField.InternalName != "LinkFilename")
                {
                    viewFields.Append("<FieldRef Name="");
                    viewFields.Append(currentField.InternalName);
                    viewFields.Append("" Nullable="TRUE" />");
                }
            }
            return viewFields.ToString();
        }
    }
}