Пример #1
0
        public static List <SearchColumn> GetSearchCriteriaList(this EntityBase obj, string operand)
        {
            List <SearchColumn> searchColumns = new List <SearchColumn>();

            PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetSetMethod() != null && propertyInfo.GetSetMethod().IsPublic&& propertyInfo.DeclaringType != typeof(EntityBase))
                {
                    object val = propertyInfo.GetValue(obj, null);
                    if (val != null && !string.IsNullOrEmpty(val.ToString()))
                    {
                        if (propertyInfo.DeclaringType != typeof(System.String) && val.ToString() == "0")
                        {
                        }
                        else
                        {
                            SearchColumn searchColumn = new SearchColumn();
                            searchColumn.Name     = propertyInfo.Name;
                            searchColumn.Value    = val.ToString();
                            searchColumn.Operand  = "=";
                            searchColumn.Criteria = operand;
                            searchColumn.DataType = val.GetType().ToString();
                            searchColumns.Add(searchColumn);
                        }
                    }
                }
            }
            return(searchColumns);
        }
Пример #2
0
        public static string GetSerializedSearchCriteria(this EntityBase obj, string[] properties, string[] criteria, string operand)
        {
            List <SearchColumn> searchColumns = new List <SearchColumn>();

            if (properties.Length != criteria.Length)
            {
                return(null);
            }
            int i = 0;

            foreach (string prop in properties)
            {
                PropertyInfo propInfo = obj.GetType().GetProperty(prop);
                if (propInfo == null)
                {
                    throw new Exception(string.Format("Property '{0}' doesn't exist in the entity", prop));
                }
                object       val          = propInfo.GetValue(obj, null);
                SearchColumn searchColumn = new SearchColumn();
                searchColumn.Name     = propInfo.Name;
                searchColumn.Value    = val.ToString();
                searchColumn.Operand  = criteria[i];
                searchColumn.Criteria = operand;
                searchColumn.DataType = val.GetType().ToString();
                searchColumns.Add(searchColumn);
                i++;
            }

            if (searchColumns.Count > 0)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                return(serializer.Serialize(searchColumns));
            }
            else
            {
                return(string.Empty);
            }
        }