1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using JLHHJSvr.Com.Model;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- namespace DirectService.Tools
- {
- internal static class ListEx
- {
- /// <summary>
- /// 把列表转换为(*,*,*,*,...)字符串,空列表返回null
- /// </summary>
- public static string getString<T>(List<T> list)
- {
- string listString;
-
- if (list==null||list.Count<=0)
- {
- return null;
- }
- StringBuilder sb = new StringBuilder("(");
- switch (typeof(T).Name)
- {
- case "DateTime":
- case "String":
- foreach (var item in list)
- {
- sb.Append("'" + item + "',");
- }
- break;
- default:
- foreach (var item in list)
- {
- sb.Append(item + ",");
- }
- break;
- }
- listString = sb.ToString().TrimEnd(',')+')';
- return listString;
- }
- public static string GetWhereStr(List<string> whereList, string connector = "AND")
- {
- string connectStr = " " + connector + " ";
- var whereStr = string.Empty;
- for (int i = 0; i < whereList.Count; i++)
- {
- if (i == 0)
- {
- whereStr += "(" + whereList[i] + ")";
- }
- else
- {
- whereStr += connectStr + "(" + whereList[i] + ")";
- }
- }
- return whereStr;
- }
- public static void GetRecursions<T>(List<T> TList, PkName pkName, out List<Recursion<T>> ReList)
- {
- ReList = new List<Recursion<T>>();
- var nodeDict = new Dictionary<int, Recursion<T>>();
- foreach(var parent in TList)
- {
- int value = Convert.ToInt32(parent.GetType().GetProperty(pkName.MainField).GetValue(parent));
- var recursion = new Recursion<T>()
- {
- data = parent
- };
- recursion.value = Convert.ToInt32(parent.GetType().GetProperty(pkName.MainField).GetValue(parent));
- recursion.text = Convert.ToString(parent.GetType().GetProperty(pkName.MainName).GetValue(parent));
- nodeDict[value] = recursion;
- }
- // 添加子节点
- foreach (var child in TList)
- {
- int value = Convert.ToInt32(child.GetType().GetProperty(pkName.MainField).GetValue(child));
- int parentid = Convert.ToInt32(child.GetType().GetProperty(pkName.ParentField).GetValue(child));
- if (parentid == 0)
- {
- ReList.Add(nodeDict[value]);
- }else if (nodeDict.TryGetValue(parentid, out var parentRecursion))
- {
- parentRecursion.AddChild(nodeDict[value]);
- }
- }
- }
- }
- }
|