1

我的最终目标是将 linq 表达式转换为获取 xml。我打算在 MS CRM 2011 中将 fetch XML 用于自定义网格。

从这篇文章中,我可以将 QueryExpression 转换为 FetchXML http://mileyja.blogspot.com/2011/07/convert-queryexpression-to-fetchxml.html

但我不知道如何将 Linq 表达式转换为 QueryExpression。当我说 linq 表达式时,我指的是 IQueryable<> 类型的对象。

任何人都有这方面的经验或知道如何?

干杯

4

3 回答 3

4

提供的解决方案不适用于 Chielus 提到的 CRM2013 SDK 程序集。

对于 CRM2013,您可以使用此扩展方法:

public static class QueryProviderExtensions
{
    public static QueryExpression ToQueryExpression<T>(this IQueryable<T> items)
    {
        var queryProvider = items.Provider;

        var queryProviderType = queryProvider.GetType();
        var listType = typeof(List<>);

        var projectionType = queryProviderType.Assembly.GetType("Microsoft.Xrm.Sdk.Linq.QueryProvider+Projection");
        var navigationSourceType = queryProviderType.Assembly.GetType("Microsoft.Xrm.Sdk.Linq.QueryProvider+NavigationSource");
        var linkLookupType = queryProviderType.Assembly.GetType("Microsoft.Xrm.Sdk.Linq.QueryProvider+LinkLookup");
        var linkLookupListType = listType.MakeGenericType(linkLookupType);

        object projection = null;
        object source = Activator.CreateInstance(navigationSourceType, new object[] { null, null });
        object linkLookups = Activator.CreateInstance(linkLookupListType);
        bool throwIfSequenceIsEmpty = false;
        bool throwIfSequenceNotSingle = false;

        object[] arguments = new object[6];
        arguments[0] = items.Expression;
        arguments[1] = throwIfSequenceIsEmpty;
        arguments[2] = throwIfSequenceNotSingle;
        arguments[3] = projection;
        arguments[4] = source;
        arguments[5] = linkLookups;

        var getQueryExpressionMethod = queryProviderType.GetMethod("GetQueryExpression", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, new[] {
                typeof(Expression), 
                typeof(bool).MakeByRefType(), 
                typeof(bool).MakeByRefType(),
                projectionType.MakeByRefType(),
                navigationSourceType.MakeByRefType(),
                linkLookupListType.MakeByRefType(),
            }, null);

        var queryExpression = (QueryExpression)getQueryExpressionMethod.Invoke(queryProvider, arguments);
        return queryExpression;
    }

    public static string ToXml(this QueryExpression queryExpression, IOrganizationService service)
    {
        var request = new QueryExpressionToFetchXmlRequest { Query = queryExpression };
        var response = (QueryExpressionToFetchXmlResponse)service.Execute(request);
        return response.FetchXml;
    }
}

像这样使用它:

    // This can be any query
    var query = (from e in contactSet.Query()
                 where e.LastName.Contains("e")
                 select e);

    // This is where the magic happens
    var queryExpression = query.ToQueryExpression();

    // We can now add paging info
    queryExpression.PageInfo = new PagingInfo()
    {
        PageNumber = 1,
        Count = 50,
        ReturnTotalRecordCount = true
    };

    // This will create a QueryExpressionToFetchXmlRequest, not needed for paging.
    var xml = queryExpression.ToXml(service);

    // Perform the actual request 
    var collection = service.RetrieveMultiple(queryExpression);

更多细节在这里:

https://social.msdn.microsoft.com/Forums/en-US/0026f1de-bc10-428d-ad15-18d34315c814/convert-linq-query-to-fetchxml-sdk-2013?forum=crm

于 2014-10-25T14:25:49.757 回答
2

您可以使用反射来获取QueryExpression,从IQuerable<T>获取提供者并在其上调用 Translate 方法。

这是我正在使用的扩展方法:

public static QueryExpression ToQueryExpression(this IQueryable @this)
{
    var provider = @this.Provider;
    var translateMethod = provider.GetType().GetMethod("Translate");
    var query = (QueryExpression)translateMethod.Invoke(provider, new object[] { @this.Expression });

    return query;
}

如果您在 C# 中使用某种动态包装器进行反射,例如ExposedObject,您可以这样做:

public static QueryExpression ToQueryExpression(this IQueryable @this)
{
    dynamic provider = ExposedObject.From(@this.Provider);

    return provider.Translate(@this.Expression);
}

至于 fetch 你需要有QueryExpression然后发出一个QueryExpressionToFetchXmlRequest返回QueryExpressionToFetchXmlResponse响应的请求,它有属性FetchXml

于 2012-06-26T10:25:33.737 回答
0

在 LINQ 表达式上调用 ToString() 应返回 FetchXML

于 2011-08-05T20:09:29.753 回答