Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Expression And Filter

public async Task<UserData> GetUserByName(string name)

​    {

​      BinaryExpression filter = null;
​      var data = new Dictionary<string, string>();
​      data.Add("name", name);
​      data.Add("technology", ".Net");
​      var userData = Expression.Parameter(typeof(UserData));
​      foreach (var toProcess in data)
​      {
​        var memberExpression = Expression.PropertyOrField(userData, toProcess.Key);
​        var constantExpression = Expression.Constant(toProcess.Value);
​        var binaryExpression = Expression.Equal(memberExpression, constantExpression); 
​        filter = (filter != null) ? Expression.And(filter, binaryExpression) : binaryExpression;

​      } 
​      Expression<Func<UserData, bool>> lambda = Expression.Lambda<Func<UserData, bool>>(filter, userData);
​      return await _dbContext.UserData.Where(lambda).FirstOrDefaultAsync();
​    }
Source by www.partech.nl #
 
PREVIOUS NEXT
Tagged: #Expression #And #Filter
ADD COMMENT
Topic
Name
2+6 =