Пример #1
0
        /// <summary>
        /// Generates the conditional query.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listDtoObject">The list dto object.</param>
        /// <returns></returns>
        /// <author>Mauricio Suárez Robelto</author>

        #region  ConditionalQuery
        public static Expression <Func <U, bool> > GeneratePredicateQuery <U, T>(List <T> listDtoObject) where T : BaseDto
        {
            var predicate = PredicateBuilder.False <U>();

            if (listDtoObject.Count() > 0)
            {
                string referenceWildcard       = "ReferenceTable";
                int    lenghtReferenceWildcard = referenceWildcard.Length;
                string typeWildcard            = "System.Nullable`1[System.Boolean]";

                PropertyInfo[] properties = listDtoObject[0].GetType().GetProperties();

                int CurrentObject = 0;

                foreach (object item in listDtoObject)
                {
                    CurrentObject++;

                    var predicateNew = PredicateBuilder.True <U>();

                    foreach (PropertyInfo property in properties)
                    {
                        if (ConditionalQuery.IsGenericType(property.PropertyType))
                        {
                            if (property.GetValue(item, null) != null)
                            {
                                bool resultIsFilter = true;
                                if (property.PropertyType.ToString() == typeWildcard)
                                {
                                    string validateFilter = property.Name;
                                    if ((validateFilter.Substring(0, validateFilter.Length < lenghtReferenceWildcard ? validateFilter.Length : lenghtReferenceWildcard)) == referenceWildcard)
                                    {
                                        resultIsFilter = false;
                                    }
                                }

                                if (resultIsFilter)
                                {
                                    ParameterExpression carParam = Expression.Parameter(typeof(U));

                                    Expression aLeft  = Expression.PropertyOrField(carParam, property.Name);
                                    Expression aRight = Expression.Constant(property.GetValue(item, null));
                                    Expression makeEqualsPredicate = Expression.Equal(aLeft, aRight);

                                    Expression <Func <U, bool> > predicate1 = Expression.Lambda <Func <U, bool> >(makeEqualsPredicate, carParam);
                                    predicateNew = predicateNew.And(predicate1);
                                }
                            }
                        }
                    }

                    predicateNew.Compile();
                    predicate = predicate.Or(predicateNew.Expand());
                }
            }

            predicate.Compile();

            return(predicate);
        }
Пример #2
0
        /// <summary>
        /// Validates the includes.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="includes">The includes.</param>
        /// <param name="typeWildcard">The type wildcard.</param>
        /// <param name="parentTable">The parent table.</param>
        /// <author>Mauricio Suárez Robelto</author>
        public static void ValidateIncludes(object item, List <string> includes, string typeWildcard, string parentTable)
        {
            if (item != null)
            {
                PropertyInfo[] properties = item.GetType().GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    if (property.GetValue(item, null) != null)
                    {
                        if (ConditionalQuery.IsGenericType(property.PropertyType))
                        {
                            if (property.PropertyType.ToString() == typeWildcard)
                            {
                                bool?valueProperty = (bool?)property.GetValue(item, null);

                                if (valueProperty.Value)
                                {
                                    includes.Add(parentTable + "." + property.Name);
                                }
                            }
                        }
                        else
                        {
                            if (property.PropertyType.IsGenericType)
                            {
                                IList listNewObject = (IList)property.GetValue(item, null);

                                if (listNewObject.Count > 0)
                                {
                                    ConditionalQuery.ValidateIncludes(listNewObject[0], includes, typeWildcard, listNewObject[0].GetType().Name);
                                }
                            }
                            else
                            {
                                ConditionalQuery.ValidateIncludes(property.GetValue(item, null), includes, typeWildcard, property.GetValue(item, null).GetType().Name);
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Generates the parameters conditional query.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listDtoObject">The list dto object.</param>
        /// <returns></returns>
        /// <author>Mauricio Suárez Robelto</author>
        public static ObjectParameter[] GenerateParametersConditionalQuery <T>(List <T> listDtoObject) where T : BaseDto
        {
            List <ObjectParameter> parameters = new List <ObjectParameter>();

            if (listDtoObject.Count() > 0)
            {
                PropertyInfo[] properties    = listDtoObject[0].GetType().GetProperties();
                int            CurrentObject = 0;

                foreach (object item in listDtoObject)
                {
                    CurrentObject++;
                    foreach (PropertyInfo property in properties)
                    {
                        if (ConditionalQuery.IsGenericType(property.PropertyType))
                        {
                            if (property.GetValue(item, null) != null)
                            {
                                string validate = property.PropertyType.ToString();
                                if (validate == "System.Nullable`1[System.DateTime]")
                                {
                                    DateTime filterDate = (DateTime)property.GetValue(item, null);
                                    parameters.Add(new ObjectParameter(property.Name + "Ini" + CurrentObject.ToString(), new DateTime(filterDate.Year, filterDate.Month, filterDate.Day, 0, 0, 0)));
                                    parameters.Add(new ObjectParameter(property.Name + "End" + CurrentObject.ToString(), new DateTime(filterDate.Year, filterDate.Month, filterDate.Day, 23, 59, 59)));
                                }
                                else
                                {
                                    parameters.Add(new ObjectParameter(property.Name + CurrentObject.ToString(), property.GetValue(item, null)));
                                }
                            }
                        }
                    }
                }
            }

            return(parameters.ToArray());
        }
Пример #4
0
        /// <summary>
        /// Generates the conditional query.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listDtoObject">The list dto object.</param>
        /// <returns></returns>
        /// <author>Mauricio Suárez Robelto</author>
        public static string GenerateConditionalQuery <T>(List <T> listDtoObject) where T : BaseDto
        {
            if (listDtoObject.Count() > 0)
            {
                string referenceWildcard       = "ReferenceTable";
                int    lenghtReferenceWildcard = referenceWildcard.Length;
                string typeWildcard            = "System.Nullable`1[System.Boolean]";

                StringBuilder sbResult = new StringBuilder();
                StringBuilder sb       = new StringBuilder();

                PropertyInfo[] properties = listDtoObject[0].GetType().GetProperties();

                int CurrentObject = 0;
                foreach (object item in listDtoObject)
                {
                    CurrentObject++;

                    int i = 1;
                    foreach (PropertyInfo property in properties)
                    {
                        if (ConditionalQuery.IsGenericType(property.PropertyType))
                        {
                            if (property.GetValue(item, null) != null)
                            {
                                bool resultIsFilter = true;
                                if (property.PropertyType.ToString() == typeWildcard)
                                {
                                    string validateFilter = property.Name;
                                    if ((validateFilter.Substring(0, validateFilter.Length < lenghtReferenceWildcard ? validateFilter.Length : lenghtReferenceWildcard)) == referenceWildcard)
                                    {
                                        resultIsFilter = false;
                                    }
                                }

                                if (resultIsFilter)
                                {
                                    string validate = property.PropertyType.ToString();
                                    if (validate == "System.Nullable`1[System.DateTime]")
                                    {
                                        sb.Append((i != 1 ? " AND (" : ""));
                                        sb.Append("it." + property.Name).Append(" >= ").Append(("@" + property.Name + "Ini" + CurrentObject.ToString()));
                                        sb.Append(" AND " + "it." + property.Name).Append(" <= ").Append(("@" + property.Name + "End" + CurrentObject.ToString()));
                                        sb.Append((i != 1 ? ")" : ""));
                                        i++;
                                    }
                                    else
                                    {
                                        sb.Append((i != 1 ? " AND " : ""));
                                        sb.Append("it." + property.Name).Append(" = ").Append(("@" + property.Name + CurrentObject.ToString()));
                                        i++;
                                    }
                                }
                            }
                        }
                    }

                    if (sb.Length > 0)
                    {
                        sb.Insert(0, "(");
                        sb.Insert(sb.Length, ")");
                    }

                    if (sbResult.Length > 0 && sb.Length > 0)
                    {
                        sbResult.Append(" OR ");
                    }
                    sbResult.Append(sb);
                    sb.Clear();
                }


                if (sbResult.Length == 0)
                {
                    sbResult.Append("1 = 1");
                }

                return(sbResult.ToString());
            }
            else
            {
                return("1 = 1");
            }
        }