2009-07-31 193 views
7

我已經避免使用fetchxml,因爲我不確定調用crmService.Fetch(fetchXml)後處理結果數據的最佳方式。在一對夫婦的情況下,我已經使用LINQ一個XDocument來檢索該數據結構中的數據,如:你如何處理fetchxml結果數據?

XDocument resultset = XDocument.Parse(_service.Fetch(fetchXml)); 
if (resultset.Root == null || !resultset.Root.Elements("result").Any()) 
{ 
    return; 
} 
foreach (var displayItem in resultset.Root.Elements("result").Select(item => item.Element(displayAttributeName)).Distinct()) 
{ 
    if (displayItem!= null && displayItem.Value != null) 
    { 
     dropDownList.Items.Add(displayItem.Value);  
    } 
} 

什麼是處理fetchxml結果數據的最佳方式,以便它可以很容易地使用。諸如將這些記錄傳遞到ASP.NET數據網格等應用程序將非常有用。

回答

1

我通常會因爲這個原因避免使用FetchXML。您可以使用RetrieveMultiple來獲取強類型的BusinessEntity對象,並且基本上執行相同的操作。

但是,如果你想使用FetchXML這個樣本應包括你:

http://msdn.microsoft.com/en-us/library/ms914457.aspx

+0

是的,我一直主要使用RetrieveMultiple,但在這種情況下,我需要檢索一些屬性並添加一些基於聯合實體的條件,fetchXml允許我這樣做,並且QueryExpression對象不允許。 – 2009-07-31 01:52:37

+0

盧克,你確定嗎?通過QueryExpression檢索也可以定義聯接。 – okutane 2009-07-31 02:04:10

+0

是的,你可以定義連接,但我相當肯定你不能從一個連接的實體返回屬性。 – 2009-07-31 02:29:09

6

我喜歡FetchXML的靈活性,所以我開發了以下函數,它返回一個數據表,用於綁定到網格和中繼器等等。

 /// <summary> 
    /// Takes a CRM FetchXML query and returns a DataTable 
    /// </summary> 
    /// <param name="fetchXml">The FetchXML query</param> 
    /// <param name="requiredFields">A array of columns you'd expect returned. This is required as if there is no data for a field/column CRM will not return it which could impact databinding</param> 
    /// <returns>A datatable containing the results of the FetchXML</returns> 
    public static DataTable FetchXML2DataTable(string fetchXml, string[] requiredFields) 
    { 
     CrmService tomService = new CrmService(); 
     tomService = CrmWebService; 

     string result = tomService.Fetch(fetchXml); 
     DataSet ds = new DataSet(); 

     System.IO.StringReader reader = new System.IO.StringReader(result); 
     ds.ReadXml(reader); 

     DataTable dt = ds.Tables[1]; 

     //check all required columns are present otherwise add them to make life easier for databinding at the top level 
     //caused by CRM not returning fields if they contain no data 
     foreach (string field in requiredFields) 
     { //Check for column names and nested tables 
      if ((dt.Columns.IndexOf(field) < 0) && (dt.DataSet.Tables.IndexOf(field) <0)) 
      {      
       //Add column to datatable even though it is empty for reason stated above 
       dt.Columns.Add(new DataColumn(field)); 
      } 
     }    

     return dt; 
    } 

的使用requiredfields字符串數組是存在的,因爲如果您的結果集包含與該列沒有數據列不回來,但是我可能要取代柱,將結合數據網格等

的確切原因

CrmService是一個啓動web服務的單例類。

希望這對你有用。

0

如果你想使用fetchxml並得到BusinessEntityType的returnset,您可以使用 FetchXmlToQueryExpression方法從fetchxml得到一個查詢表達式,然後在RetrieveMultiple方法應用於查詢表達式作爲

FetchXmlToQueryExpressionResponse qe = (FetchXmlToQueryExpressionResponse) service.Execute(fetch); 

請注意,反向方法QueryExpressiontoFetchXml存在以及

1

隨着QueryExpression不能查詢多到許多實體,不能從一個以上的實體一次檢索屬性,因此必須使用FetchXML。

不幸的是,LinqToCRM的codeplex項目已經過時(1年沒有新的版本,但它似乎是一個很好的實現,比微軟的版本更好),包含一個linq動態鏈接提供程序crm,但我閱讀了一些關於這個新版本的文章,它不太好,似乎是一個有很多限制(強制緩存等)的「糟糕的實現」。

我看到很多人使用LinqToXML和DataSet來領導FetchXML結果,但我無法說出處理它的最佳方式。你怎麼看待這件事?

Christophe Trevisani Chavey。