/// <summary> /// ConditionBuilder 并不支持生成Like操作,如 字符串的 StartsWith,Contains,EndsWith 并不能生成这样的SQL: Like ‘xxx%’, Like ‘%xxx%’ , Like ‘%xxx’ . 只要override VisitMethodCall 这个方法即可实现上述功能。 /// </summary> /// <param name="m"></param> /// <returns></returns> protected override Expression VisitMethodCall(MethodCallExpression m) { string connectorWords = GetLikeConnectorWords(m_dataBaseType); //获取like链接符 if (m == null) { return(m); } string format; switch (m.Method.Name) { case "StartsWith": format = "({0} LIKE ''" + connectorWords + "{1}" + connectorWords + "'%')"; break; case "Contains": format = "({0} LIKE '%'" + connectorWords + "{1}" + connectorWords + "'%')"; break; case "EndsWith": format = "({0} LIKE '%'" + connectorWords + "{1}" + connectorWords + "'')"; break; case "Equals": // not in 或者 in 或 not like format = "{0} {1} "; break; case "Parse": format = "{1}"; break; case "IsNull": if (m.Method.ReflectedType == typeof(SqlColumnUtil)) { object obj = TypeUtil.GetSqlDataTypeValue(m.Arguments[1], m.Arguments[1].Type); format = "isnull({1}," + obj.ToString().Replace("\"", "") + ")"; } else { format = "({1})"; throw new NotSupportedException(m.NodeType + " is not supported!"); } break; default: if (m.Method.ReturnType == typeof(int)) { if (DataBaseType.SqlServer.Equals(m_dataBaseType)) { format = "convert(int,{1})"; break; } } throw new NotSupportedException(m.NodeType + " is not supported!"); } if (m.Object != null) { this.Visit(m.Object); } this.Visit(m.Arguments[0]); string right = this.m_conditionParts.Pop(); string left = string.Empty; if (m.Object != null) { left = this.m_conditionParts.Pop(); } this.m_conditionParts.Push(string.Format(format, left, right)); return(m); }
/// <summary> /// 实体列表转DataTable /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="modelList">实体列表</param> /// <returns></returns> public static DataTable ModelList2DataTable <T>(List <T> modelList) { if (modelList == null) { return(null); } DataTable dataTable = new DataTable(); var model = modelList[0]; Type t = model.GetType();//获得该类的Type //创建表结构、设置字段信息 foreach (PropertyInfo pi in t.GetProperties()) { DataColumn dataColumn = new DataColumn(); ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 if (attr != null) { dataColumn.ColumnName = attr.ColumnName; dataColumn.DataType = TypeUtil.GetType(attr.ColumnType); dataColumn.Caption = attr.ColumnTitle; if (attr.IsPrimaryKey) { dataColumn.Unique = true; } } else { object value = pi.GetValue(model, null); dataColumn.ColumnName = pi.Name; if (value != null) { dataColumn.DataType = value.GetType(); } //else //{ // dataColumn.DataType = typeof(string); //} } dataTable.Columns.Add(dataColumn); } //添加行数据 foreach (var m in modelList) { object[] values = new object[t.GetProperties().Length]; int idx = 0; foreach (PropertyInfo pi in t.GetProperties()) { object value = pi.GetValue(model, null); //ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 //if (attr != null && !string.IsNullOrEmpty(attr.ColumnType)) //{ // value = pi.GetValue(model, null); //} //else //{ // value = pi.GetValue(model, null); //} values[idx] = value; idx++; } dataTable.Rows.Add(values); } return(dataTable); }