【问题标题】:How do you handle the fetchxml result data?您如何处理 fetchxml 结果数据?
【发布时间】:2010-11-15 15:43:10
【问题描述】:

我一直避免使用 fetchxml,因为我不确定在调用 crmService.Fetch(fetchXml) 后处理结果数据的最佳方式。在几种情况下,我使用了带有 LINQ 的 XDocument 从该数据结构中检索数据,例如:

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 数据网格等应用程序将非常有用。

【问题讨论】:

    标签: xml data-structures dynamics-crm data-manipulation fetchxml


    【解决方案1】:

    出于这个原因,我通常避免使用 FetchXML。您可以使用 RetrieveMultiple 来获取强类型的 BusinessEntity 对象并基本上做同样的事情。

    但是,如果您想使用 FetchXML,这个示例应该涵盖您:

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

    【讨论】:

    • 是的,我一直主要使用 RetrieveMultiple,但在这种情况下,我需要检索一些属性并添加一些基于连接实体的条件,而 fetchXml 将允许我这样做,而 QueryExpression 对象不会允许。
    • 卢克,你确定吗? QueryExpression 检索也有定义连接的方法。
    • 是的,您可以定义连接,但我很确定您不能从连接实体返回属性。
    【解决方案2】:

    您也可以选择 LinqtoCRM,它会为您处理 XML 解析: http://codeplex.com/linqtocrm

    【讨论】:

      【解决方案3】:

      我喜欢 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 服务的单例类。

      希望这对你有用。

      【讨论】:

      • +1 太好了,感谢您说出最清晰的答案(将返回的 XML 转换为表格)。
      • 嗨 Fishcake,这看起来很棒 - 我在行 tomService = CrmWebService; 时遇到错误“当前上下文中不存在名称‘CrmWebService’” - 请问有什么想法吗?谢谢!
      • 注意string result = tomService.Fetch(fetchXml); 不工作CRM 2016CRM 2011 中已弃用
      【解决方案4】:

      如果您想使用 fetchxml 并获取 BusinessEntityType 的返回集,您可以使用 FetchXmlToQueryExpression 方法从 fetchxml 中获取查询表达式,然后在 RetrieveMultiple 方法中应用查询表达式,如

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

      请注意,反向方法 QueryExpressiontoFetchXml 也存在

      【讨论】:

        【解决方案5】:

        使用 QueryExpression,您不能查询多对多实体,也不能一次从多个实体中检索属性,因此您必须使用 FetchXML。

        不幸的是,随着 CRM SDK 4.0.12 的发布,LinqToCRM 的 codeplex 项目已经过时(1 年没有新版本,但它似乎是一个很好的实现,比微软的版本更好),其中包含动态 crm 的 linq 提供程序,但我读了一些关于这个新版本的文章,它不是很好,似乎是一个“糟糕的实现”,有很多限制(强制缓存等)。

        我看到很多人使用 LinqToXML 和 DataSet 来引导 FetchXML 结果,但我不能说处理它的最佳方法是什么。您对此有何看法?

        克里斯托夫·特雷维萨尼·查维。

        【讨论】:

        • 其实是默认缓存,在配置文件中添加一些配置选项可以关闭。仅供参考
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        相关资源
        最近更新 更多