using LFlow.Base.Interfaces; namespace LFlow.Base.Utils; public static class ObjectToWhereExp { /// /// 将IModel对象转换为where条件 /// /// /// public static string ToWhereExp(this IModel model) { var type = model.GetType(); var properties = type.GetProperties(); var whereExp = " 1=1 "; foreach (var property in properties) { var value = property.GetValue(model); if (value == null) continue; if (property.PropertyType == typeof(string)) { whereExp += $" and {property.Name} like '%{value}%'"; } else if (property.PropertyType == typeof(DateTime) && value is DateTime dateTime) { if (dateTime != default(DateTime)) { whereExp += $" and {property.Name} like '%{value}%'"; } } else if (property.PropertyType.IsEnum) { var enumValue = (int)value; whereExp += $" and {property.Name} = '{enumValue}'"; } else if (property.PropertyType == typeof(bool) && value is bool flag) { if (flag) { whereExp += $" and {property.Name} = 1"; } else { whereExp += $" and {property.Name} = 0"; } } else if (value != null) { whereExp += $" and {property.Name} = '{value}'"; } } return whereExp; } }