How can I add a specific dictionary entry to a Linq Expression?

c# dynamic-linq linq linq-expressions


I'm trying to convert a JSON representation of the query into an executable Linq query using the dynamic-linq-query-builder library. As it is, I don't think this library supports dictionaries, thus I'm seeking to add that feature.

This is how my JSON looks:

  "condition": "AND",
  "rules": [
      "id": "Field_1",
      "field": "Data[\"Field_1\"].StringValue",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "test"
  "valid": true

and I'm attempting to create the following Linq query:

.Where(p => p.Data["Field_1"].StringValue == "test")

I'm having a lot of difficulty coming up with anSystem.Linq.Expressions.Expression This reflects this dictionary entry, which is practically hard-coded (theData["Field_1"] ) a section of the tree.

I've looked at this StackOverflow topic (how-do-i-access-a-dictionary-item-using-linq-expressions) and read several publications, but the answer still eludes me.

Can someone please assist me in the proper path? The issue is showing up in theBuildNestedExpression() method of the class this.

I appreciate it.

Aktuelles 13/12/19

I feel like I'm losing my mind quickly, despite the fact that I'm making some work on this. As indicated above, the issue appears in theBuildNestedExpression() currently does not allow dictionary properties in method. I'm attempting to make this more supported.

I recognize that my code might be much better (I was only trying to get it to function at first), but my modifications are in GitHub as here.

I've changed to get out of the rut.BuildNestedExpression() If I include a dictionary into my new approach,BuildElementAccessExpression() . Here, I'm only attempting to supplement the expression being constructed for a property with any dictionary entries. However, I'm becoming quite perplexed in the process (as you can probably tell by the commented out code).

I've included a little unit test to highlight the issue (Dictionary_Test ).

I won't upload lengthy code snippets here since GitHub has them already. I truly appreciate any assistance because I don't believe I can figure this one out on my own.

12/13/2019 3:15:09 PM

Accepted Answer

With this issue, I eventually arrived. The procedure a little bit damaged my brain, but you can find the expression's functioning code fragment below.

We can only hope that someone will benefit from this in the future.

I appreciate your assistance, @NetMage.

var propertyInfo = expression.Type.GetProperty("Data");
var propertyExpression = Expression.Property(expression, propertyInfo);

var dictionaryKeyValue = "dictionaryEntryKey";
var dictionaryKeyConst = Expression.Constant(dictionaryKeyValue);

var dictionary = propertyType.GetInterface("IDictionary`2");
PropertyInfo indexerProperty = dictionary.GetProperty("Item");

var indexExpression = Expression.MakeIndex(propertyExpression, indexerProperty, new[] { dictionaryKeyConst });

1/2/2020 12:56:42 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow