基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,283 @@
|
||||
/******************************
|
||||
* 说明:DataTable相关扩展方法
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2017-11-15
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// DataTable相关扩展方法
|
||||
/// </summary>
|
||||
public static class DataTableExtend
|
||||
{
|
||||
private static TaskSchedulerUtil taskSchedulerUtil = new TaskSchedulerUtil(20);
|
||||
/// <summary>
|
||||
/// <para>说明:移除DataTable中DataRow空白行</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-15 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="rows">The rows.</param>
|
||||
public static DataTable TrimEmptyRows(this DataTable table)
|
||||
{
|
||||
//GridView.DeleteSelectedRows()只是改变了数据状态为delete,数据依然存在于table中需要更新
|
||||
table.AcceptChanges();
|
||||
if (table == null) return table;
|
||||
|
||||
int rowCount = table.Rows.Count;
|
||||
for (int i = rowCount - 1; i > 0; i--)
|
||||
{
|
||||
bool isNull = true;
|
||||
DataRow item = table.Rows[i];
|
||||
for (int j = 0; j < table.Columns.Count; j++)
|
||||
{
|
||||
DataColumn col = table.Columns[j];
|
||||
if (!string.IsNullOrWhiteSpace(item[col.ColumnName] + ""))
|
||||
{
|
||||
isNull = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isNull)
|
||||
{
|
||||
table.Rows.Remove(item);
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:移除DataTable中已删除的行</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable TrimDeleteRows(this DataTable table)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0) return table;
|
||||
|
||||
DataRow[] deleteRows = table.Rows.Cast<DataRow>().Where(x => x.RowState == DataRowState.Deleted).ToArray();
|
||||
|
||||
for (int i = 0; i < deleteRows.Length; i++)
|
||||
{
|
||||
table.Rows.Remove(deleteRows[i]);
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:按field排序DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <param name="field">The field.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable OrderBy(this DataTable table, string field, bool isAccept = true)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table;
|
||||
|
||||
if (isAccept) table.AcceptChanges();
|
||||
|
||||
table = table.TrimDeleteRows();
|
||||
|
||||
for (int i = 0; i < table.Rows.Count; i++)
|
||||
{
|
||||
table.Rows[i][field] = (i + 1);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
public static DataTable OrderBy(this DataTable table, string field, int startIndex, int endIndex)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field) || startIndex < 0) return table;
|
||||
table.AcceptChanges();//此处提交修改会有问题,后续保存时无法获取多次修改的状态
|
||||
table = table.TrimDeleteRows();
|
||||
|
||||
//for (int i = 0; i < table.Rows.Count; i++)
|
||||
//{
|
||||
// table.Rows[i][field] = (i + 1);
|
||||
//}
|
||||
//int startUpdateIndex = 0;
|
||||
//int endUpdateIndex = 0;
|
||||
//if (endIndex < 0)
|
||||
//{
|
||||
// startUpdateIndex = startIndex;
|
||||
// endUpdateIndex = table.Rows.Count - 1;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// startUpdateIndex = startIndex < endIndex ? startIndex : endIndex;
|
||||
// endUpdateIndex = startIndex < endIndex ? endIndex : startIndex;
|
||||
//}
|
||||
//startUpdateIndex = selectSourceIndex != null && selectSourceIndex.Length > 0 ? startUpdateIndex < selectSourceIndex.Min() ? startUpdateIndex : selectSourceIndex.Min() : startUpdateIndex;
|
||||
//endUpdateIndex = selectSourceIndex != null && selectSourceIndex.Length > 0 ? endUpdateIndex > selectSourceIndex.Max() ? endUpdateIndex : selectSourceIndex.Max() : endUpdateIndex;
|
||||
//每次拖动排序后都会改变行状态,导致保存时只会保存最后拖动改变的行,改为全部排序。
|
||||
int startUpdateIndex = 0;
|
||||
int endUpdateIndex = table.Rows.Count - 1;
|
||||
for (int i = startUpdateIndex; i <= endUpdateIndex; i++)
|
||||
{
|
||||
table.Rows[i][field] = i + 1;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:按配置条件排序DataTable</para>
|
||||
/// <para>创建人:王一帆</para>
|
||||
/// <para>创建日期:2020-07-03 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <param name="field">The field.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable OrderBy(this DataTable table, string field, string UpperFieldName, int RowHandle)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table;
|
||||
|
||||
table.AcceptChanges();
|
||||
table = table.TrimDeleteRows();
|
||||
if (RowHandle == 0)
|
||||
{
|
||||
for (int i = RowHandle; i < table.Rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrEmpty(table.Rows[i][UpperFieldName] + "")) continue;
|
||||
table.Rows[i + 1][field] = table.Rows[i][UpperFieldName];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = RowHandle; i < table.Rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrEmpty(table.Rows[i - 1][UpperFieldName] + "")) continue;
|
||||
table.Rows[i][field] = table.Rows[i - 1][UpperFieldName];
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取HashTable值
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="dic"></param>
|
||||
/// <param name="control"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetValue<T>(this Dictionary<object, Hashtable> dic, object control, string key, out T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dic == null)
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
if (dic.ContainsKey(control))
|
||||
{
|
||||
if (dic[control].ContainsKey(key))
|
||||
{
|
||||
Task<T> task = dic[control][key] as Task<T>;
|
||||
value = task.Result;
|
||||
task.Dispose();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获得任务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="cachesDic"></param>
|
||||
/// <param name="control"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="task"></param>
|
||||
public static Task<T> GetTask<T>(this Dictionary<object, Hashtable> cachesDic, object control, string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (cachesDic.ContainsKey(control) && cachesDic[control].ContainsKey(key))
|
||||
{
|
||||
return (Task<T>)cachesDic[control][key];
|
||||
}
|
||||
else
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加任务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="cachesDic"></param>
|
||||
/// <param name="control"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="task"></param>
|
||||
public static Task<T> AddTask<T>(this Dictionary<object, Hashtable> cachesDic, object control, string key, Task<T> task)
|
||||
{
|
||||
Hashtable cachesHash = new Hashtable();
|
||||
if (!cachesDic.ContainsKey(control))
|
||||
{
|
||||
cachesDic.Add(control, cachesHash);
|
||||
}
|
||||
else
|
||||
{
|
||||
cachesHash = cachesDic[control];
|
||||
}
|
||||
cachesHash.Add(key, task);
|
||||
if (task.Status == TaskStatus.Created)
|
||||
{
|
||||
task.Start(taskSchedulerUtil);
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class ListExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:集合转为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-06-11 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="collection">The collection.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
IEnumerable<T> collection= list.AsEnumerable();
|
||||
var props = typeof(T).GetProperties();
|
||||
var dt = new DataTable();
|
||||
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
|
||||
if (collection.Count() > 0)
|
||||
{
|
||||
for (int i = 0; i < collection.Count(); i++)
|
||||
{
|
||||
ArrayList tempList = new ArrayList();
|
||||
foreach (PropertyInfo pi in props)
|
||||
{
|
||||
object obj = pi.GetValue(collection.ElementAt(i), null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
object[] array = tempList.ToArray();
|
||||
dt.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/******************************
|
||||
* 说明:字符串扩展
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-05-28
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class StringExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:字符串转换为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="stringData">The string data.</param>
|
||||
/// <param name="columns">The columns.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToNewDataTable(this string stringData, string columns)
|
||||
{
|
||||
DataTable dtTable = new DataTable();
|
||||
dtTable.Columns.Add(columns);
|
||||
stringData = stringData.Replace(" ", "").Replace(",,", ",").Trim(',');
|
||||
string[] confirmOper = stringData.Split(',');
|
||||
for (int i = 0; i < confirmOper.Length; i++)
|
||||
{
|
||||
DataRow row = dtTable.NewRow();
|
||||
row[columns] = confirmOper[i];
|
||||
dtTable.Rows.Add(row);
|
||||
}
|
||||
return dtTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-10-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="s">The s.</param>
|
||||
/// <param name="format">The format.</param>
|
||||
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
|
||||
public static string ToStr(this object s, string format = "")
|
||||
{
|
||||
string result = string.Empty;
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(format))
|
||||
{
|
||||
result = s.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = string.Format("{0:" + format + "}", s);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:{key}字符串替换</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="ignoreCase">是否忽略大小写</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string Replace(this string str, string key, string value, bool ignoreCase)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str)) return str;
|
||||
|
||||
return Regex.IsMatch(str, key, RegexOptions.IgnoreCase) ?
|
||||
System.Text.RegularExpressions.Regex.Replace(str, key, value, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)
|
||||
: str;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期: </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public static bool IsChinese(this string text)
|
||||
{
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
if ((int)text[i] > 127)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user