Beispiel #1
0
        /// <summary>
        /// 筛选条件
        /// </summary>
        /// <param name="conditions">条件</param>
        public MySqlDbSet <T> Where(params string[] conditions)
        {
            var set = new MySqlDbSet <T>(this);
            var whereStringBuilder = new StringBuilder();

            foreach (var kv in Dict)
            {
                foreach (string condition in conditions)
                {
                    var arr = condition.Split('=', '<', '>');
                    arr = arr.Where(x => !string.IsNullOrEmpty(x)).ToArray();
                    if (arr[0] == kv.Key)
                    {
                        string str   = condition.Remove(condition.LastIndexOf(arr[1])).Remove(0, kv.Key.Length);
                        string key   = kv.Value;
                        string value = arr[1].Trim();
                        whereStringBuilder.Append(" and ").Append(key).Append(str).Append("?").Append(key);
                        set.MySqlParameterList.Add(new MySqlParameter("?" + key, value));
                    }
                }
            }
            if (whereStringBuilder.Length > 0)
            {
                whereStringBuilder.Remove(0, 4).Insert(0, " where ");
                set.WhereString = whereStringBuilder.ToString();
            }
            return(set);
        }
Beispiel #2
0
        /// <summary>
        /// 筛选条件
        /// </summary>
        /// <param name="entity">条件</param>
        public MySqlDbSet <T> Where(T entity)
        {
            var set = new MySqlDbSet <T>(this);
            var whereStringBuilder = new StringBuilder();

            foreach (var finfos in Type.GetProperties())
            {
                var key   = finfos.Name;
                var value = Convert.ToString(Type.GetProperty(key).GetValue(entity, null));
                if (!string.IsNullOrEmpty(value))
                {
                    string columName = Dict[key];
                    whereStringBuilder.Append(" and ").Append(columName).Append("=?").Append(columName);
                    set.MySqlParameterList.Add(new MySqlParameter("?" + columName, value));
                }
            }
            if (whereStringBuilder.Length > 0)
            {
                whereStringBuilder.Remove(0, 4).Insert(0, " where ");
                set.WhereString = whereStringBuilder.ToString();
            }
            return(set);
        }