dbjoe
New member
- Joined
- May 13, 2022
- Messages
- 1
- Programming Experience
- 1-3
Hello,
I'm looking for some help with this problem that I am having. I am trying to build a dynamic where clause for Entity Framework using the Expression API. I have a dictionary that contains the following as an example:
myDictionary.Add("ColumnName1", "Column1Value")
myDictionary.Add("ColumnName2", "Column2Value")
I would like the output of the expression to be like this: "x.ColumnName1 == Column1Value AND x.ColumnName2 == Column2Value "
In my code I have tried to keep it simple and use Lists<string>, But ideally I would like to use my dictionary as shown in the above example
I would be grateful for any help on this.
I'm looking for some help with this problem that I am having. I am trying to build a dynamic where clause for Entity Framework using the Expression API. I have a dictionary that contains the following as an example:
myDictionary.Add("ColumnName1", "Column1Value")
myDictionary.Add("ColumnName2", "Column2Value")
I would like the output of the expression to be like this: "x.ColumnName1 == Column1Value AND x.ColumnName2 == Column2Value "
In my code I have tried to keep it simple and use Lists<string>, But ideally I would like to use my dictionary as shown in the above example
Sample Code:
var xParameter = Expression.Parameter(typeof(T), "x"); // x =>
var keyList = new List<string>();
var keyValuesList = new List<string>();
foreach(var (Key, value) in keyColumns)
{
keyList.Add(Key);
keyValuesList.Add(value);
}
var leftHandSides = keyList.Select(x => Expression.Property(xParameter, x));
var rightHandSide = Expression.Constant(keyValuesList, typeof(List<string>));
var equalityExpressions = leftHandSides.Select(x => Expression.Equal(x, rightHandSide)); //CODE FAILS HERE WITH BINARY ERROR
var aggregatedExpressions = equalityExpressions.Aggregate((x, y) => Expression.AndAlso(x, y));
return Expression.Lambda<Func<T, bool>>(aggregatedExpressions, xParameter);
I would be grateful for any help on this.