c#泛型结合Linq实现从数据源中截取指定位置,行数的数据

时间:2021-04-29 10:40:01
//封装方法        
/// <summary>
/// 按指定截取值
/// </summary>
/// <typeparam name="T"> </typeparam>
/// <param name="Generics">数据源</param>
/// <param name="GenericsReturn">截取数据</param>
/// <param name="NumEnd">截取区域的序号</param>
/// <param name="Interval">截取单位长度</param>
/// <returns>失败返回false</returns>
public static bool InterceptDataInterval<T>(this T[] Generics, out T[] GenericsReturn, int NumEnd = 1, int Interval = 1000)
{
Debug.Assert(NumEnd >= 1);
return (Generics.InterceptDataCore<T>(out GenericsReturn, NumEnd - 1, NumEnd, Interval));
}

/// <summary>
/// 按指定截取值
/// </summary>
/// <typeparam name="T"> </typeparam>
/// <param name="Generics">数据源</param>
/// <param name="GenericsReturn">截取数据</param>
/// <param name="NumBegin">前一截取区域的序号</param>
/// <param name="NumEnd">当前截取区域的序号</param>
/// <param name="Size">截取单位长度</param>
/// <returns>失败返回false</returns>
public static bool InterceptDataCore<T>(this T[] Generics, out T[] GenericsReturn, int NumBegin = 0, int NumEnd = 1, int Size = 1000)
{
Debug.Assert(Generics != null);
Debug.Assert(NumBegin >= 0);
Debug.Assert(NumBegin < NumEnd);

int CountBegin = NumBegin * Size;//起始行的行号
int CountGet = (NumEnd - NumBegin) * Size;//将获取行的数量
int Count = Generics.ToArray<T>().Length;

//将取数据行的下限大于源表的行数
if (CountBegin > Count)
{
GenericsReturn = null;
return false;
}
//将取数据行的上限大于源表的行数,获取行数按源表行数重设
if (CountBegin + CountGet > Count)
CountGet = Count - CountBegin;

//截取数据行返回DataTable
GenericsReturn = Generics.AsEnumerable().Skip(CountBegin).Take<T>(CountGet).ToArray();

return true;
}
   //实际调用                string[] strsWpdm = Wpdm.Split(',').ToArray<string>();                string[] strsTemp;                //商品编码, 14  4000/14=285,提升容错率取值260:每260个商品编码启用一个线程                int unitInvoke = 260;                int cntInvoke = Math.Ceiling(strsWpdm.Length.ToDefaultableDecimal() / unitInvoke).ToDefaultableInt32();                for (int i = 1; i <= cntInvoke; i++)                {                    //公共方法截取字符串                    if (!strsWpdm.InterceptDataInterval<string>(out strsTemp, i, unitInvoke))                        continue;                   ...                }