Dynamic Linq to Xml example

倖福魔咒の 提交于 2019-12-02 02:53:57

There are two issues here really, your where clause:

("AGMNT_TYPE_CODE") == "ISDA"

... will of course evaluate to false, because they are both strings.

The second issue is that the ExpressionParser is limited in scope, it can only do comparisons on a set of predefined types. You need to recompile the Dynamic library and either allow some additional types (you can do this by modifying the predefinedTypes static field of the ExpressionParser type) or, remove the check for predefined types (which is what I have done previously):

Expression ParseMemberAccess(Type type, Expression instance)
{
  // ...
        switch (FindMethod(type, id, instance == null, args, out mb))
        {
            case 0:
                throw ParseError(errorPos, Res.NoApplicableMethod,
                    id, GetTypeName(type));
            case 1:
                MethodInfo method = (MethodInfo)mb;
                //if (!IsPredefinedType(method.DeclaringType)) // Comment out this line, and the next.
                    //throw ParseError(errorPos, Res.MethodsAreInaccessible, GetTypeName(method.DeclaringType));
                if (method.ReturnType == typeof(void))
                    throw ParseError(errorPos, Res.MethodIsVoid,
                        id, GetTypeName(method.DeclaringType));
                return Expression.Call(instance, (MethodInfo)method, args);
            default:
                throw ParseError(errorPos, Res.AmbiguousMethodInvocation,
                    id, GetTypeName(type));
        }
  // ...
}

Those lines I have commented out are where the check for predefined types are done.

Once you have made that change, you need to update your query (remember, ExpressionParser builds expressions that are compiled, so simply using "(\"AGRMNT_TYPE_CODE\") == \"ISDA\"" wont work. You would need something like:

string where = "Element(\"AGMNT_TYPE_CODE\").Value == \"ISDA\"";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!