/// <summary> /// /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="expression"></param> /// <param name="isNot"></param> /// <param name="isEqualsTo"></param> /// <returns></returns> private static QueryGroup ParseContainsForArrayOrList <TEntity>(MethodCallExpression expression, bool isNot, bool isEqualsTo) where TEntity : class { // TODO: Refactor this // Return null if there is no any arguments if (expression.Arguments?.Any() != true) { return(null); } // Get the last arg var last = expression .Arguments .LastOrDefault(); // Make sure the last arg is a member if (last == null || last?.IsMember() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported."); } // Make sure it is a property info var member = last.ToMember().Member; if (member.IsPropertyInfo() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported."); } // Get the property var property = member.ToPropertyInfo(); // Make sure the property is in the entity if (PropertyCache.Get <TEntity>().FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, property.Name, StringComparison.OrdinalIgnoreCase)) == null) { throw new InvalidExpressionException($"Invalid expression '{expression.ToString()}'. The property {property.Name} is not defined on a target type '{typeof(TEntity).FullName}'."); } // Get the values var values = (object)null; // Array/List Separation if (expression.Object == null) { // Expecting an array values = expression.Arguments.First().GetValue(); } else { // Expecting a list here values = expression.Object.GetValue(); } // Add to query fields var operation = (isNot == false && isEqualsTo == true) ? Operation.In : Operation.NotIn; var queryField = new QueryField(PropertyMappedNameCache.Get(property), operation, values); // Return the result var queryGroup = new QueryGroup(queryField); // Set the IsNot value queryGroup.SetIsNot(isNot == true && isEqualsTo == false); // Return the instance return(queryGroup); }
/// <summary> /// Gets the mapped-name for the current property. /// </summary> /// <returns>The mapped-name value.</returns> public string GetMappedName() { return(mappedName ??= PropertyMappedNameCache.Get(GetDeclaringType(), PropertyInfo)); }
/// <summary> /// /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="expression"></param> /// <param name="isNot"></param> /// <param name="isEqualsTo"></param> /// <returns></returns> private static QueryGroup ParseAllOrAnyForArrayOrAnyForList <TEntity>(MethodCallExpression expression, bool isNot, bool isEqualsTo) where TEntity : class { // TODO: Refactor this // Return null if there is no any arguments if (expression.Arguments?.Any() != true) { return(null); } // Get the last property var last = expression .Arguments .LastOrDefault(); // Make sure the last is a member if (last == null || last?.IsLambda() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported."); } // Make sure the last is a binary var lambda = last.ToLambda(); if (lambda.Body.IsBinary() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported."); } // Make sure it is a member var binary = lambda.Body.ToBinary(); if (binary.Left.IsMember() == false && binary.Right.IsMember() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported. Expression must contain a single condition to any property of type '{typeof(TEntity).FullName}'."); } // Make sure it is a property var member = binary.Left.IsMember() ? binary.Left.ToMember().Member : binary.Right.ToMember().Member; if (member.IsPropertyInfo() == false) { throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported."); } // Make sure the property is in the entity var property = member.ToPropertyInfo(); if (PropertyCache.Get <TEntity>().FirstOrDefault(p => string.Equals(p.PropertyInfo.Name, property.Name, StringComparison.OrdinalIgnoreCase)) == null) { throw new InvalidExpressionException($"Invalid expression '{expression.ToString()}'. The property {property.Name} is not defined on a target type '{typeof(TEntity).FullName}'."); } // Variables needed for fields var queryFields = new List <QueryField>(); var conjunction = Conjunction.And; // Support only various methods if (expression.Method.Name == "Any") { conjunction = Conjunction.Or; } else if (expression.Method.Name == "All") { conjunction = Conjunction.And; } // Call the method var first = expression.Arguments.First(); var values = (object)null; // Identify the type of the argument if (first.IsNewArray()) { values = first.ToNewArray().GetValue(); } else if (first.IsMember()) { values = first.ToMember().GetValue(); } // Values must be an array if (values is System.Collections.IEnumerable) { var operation = QueryField.GetOperation(binary.NodeType); foreach (var value in (System.Collections.IEnumerable)values) { var queryField = new QueryField(PropertyMappedNameCache.Get(property), operation, value); queryFields.Add(queryField); } } // Return the result return(new QueryGroup(queryFields, conjunction, (isNot == isEqualsTo))); }