0

I want to concat two array in LINQ dynamic parser.

I am sending dictionary input as below

    object a= new object[]{1d,0d,1d};
    object b = new object[]{};
    var dict = new Dictionary<string, object>();
    dict["KIStrike"] = a;
    dict["NonKIStrike"] = b;

It throws {"')' or ',' expected"} exception, when I am executing below statement

var exp = "Enumerable.Concat(dict[\"KIStrike\"] as IEnumerable<object>, dict[\"NonKIStrike\"] as IEnumerable<object>)";
var param = Expression.Parameter(typeof(Dictionary<string, object>), "dict");
var lambda = DynamicExpressionParser.ParseLambda(new[] { param }, null, exp);
var result = lambda.Compile().DynamicInvoke(dict);

Whether my expression is incorrect. Please let me know if you have any solution. Thanks in advance!!!.

ranjit powar
  • 104
  • 2
  • 2
  • 10
  • Does it fail during compile time? Should you escape quotes in exp? – fenixil Jan 05 '21 at 05:21
  • Compile time there are no issue. actually I entered this text manually that's why its coming let me update – ranjit powar Jan 05 '21 at 05:24
  • Also I checked array docs and looks like it does not implement generic interfaces: https://learn.microsoft.com/en-us/dotnet/api/system.array?view=net-5.0. – fenixil Jan 05 '21 at 05:29
  • If I execute below statement in LinqPad it works correctly Enumerable.Concat(dict["KIStrike"] as IEnumerable, dict["NonKIStrike"] as IEnumerable) – ranjit powar Jan 05 '21 at 05:33
  • is there any other way to append array in linq parser, I will try that – ranjit powar Jan 05 '21 at 05:36
  • it seems casting doesn't work in DynamicExpressions. e.g. "(dict[\"KIStrike\"] as IEnumerable) " is also giving same exception – ranjit powar Jan 05 '21 at 07:24
  • Hey, I checked the sources of the dynamic linq and I very doubt it supports what you are trying to do - cast value. Its syntaxis is documented [here](https://dynamic-linq.net/expression-language) and it is very limited. I think we might hit [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - trying to make dynamic query work for the solution which does not require dynamic query. Please elaborate in your question what are you trying to achieve, what forces you to use dynamic LINQ. – fenixil Jan 06 '21 at 05:13
  • Hi fenexil, the solution seems to be really complicated.I dont want to implement that, I found the alternate way. I am not doing concatination using dynamic expressions, but I am doing it later in XSLT file where I will actually need it. Thanks for your time!!! – ranjit powar Jan 06 '21 at 06:45

0 Answers0