sharepoint列表如何进行随机取几条记录?
sharepoint列表如何进行随机取记录?由于itemid是不连续。可能存在删除添加等操作导致
我们可以采用随机取第几条记录。把记录集合取出来。产生随机第几条数。这里关键是如何产生不重复的随机数
方法如下:
#region//获取随机数
/// <summary>
/// 获取随机数
/// </summary>
/// <param name="Number">随机个数</param>
/// <param name="minNum">随机的最小数</param>
/// <param name="maxNum">随机的最大数</param>
/// <returns></returns>
public static int[] GetRandomArray(int Number, int minNum, int maxNum){int j;
int[] b = new int[Number];Random r = new Random();
for (j = 0; j < Number; j++)
{int i = r.Next(minNum, maxNum + 1);
int num = 0;
for (int k = 0; k < j; k++){if (b[k] == i)
{num = num + 1;}}if (num == 0)
{b[j] = i;}else
{j = j - 1;}}return b;
}#endregion
//得到最大数
int maxRandom = list.Items.Count;
//最小1
int minRandom = 1;
//得到随机数组
int[] randArr = GetRandomArray(topNumber, minRandom, maxRandom);
//读取
SPQuery query = new SPQuery();
//过滤后的记录
SPListItemCollection listItemCollection = list.GetItems(query);#region//如果有记录
if (listItemCollection !=null && listItemCollection.Count>0){//foreach
//计数器
int i = 1;
//得到记录计数器
int j = 0;
foreach(SPListItem randItem in listItemCollection){#region//若计数器在随机数组里头,如果存在
if (((IList)randArr).Contains(i))
{//取记录
//******************读取id,工号,姓名,图片url,
EmployeeEntity randentity = new EmployeeEntity();
//
randentity.ItemID = randItem["ID"] == null ? string.Empty :randItem["ID"].ToString();
//工号
randentity.EmployeeNO = randItem["EmployeeNO"] == null ? string.Empty :randItem["EmployeeNO"].ToString();
//姓名
randentity.EmployeeName = randItem["EmployeeName"] == null ? string.Empty :randItem["EmployeeName"].ToString();
//*****************图片url
//
result.Add(randentity);//计数器+1
j++;}//如果已经获取50张,则退出循环
if(j == topNumber)
{break;
}//计数器+1
i++;#endregion}}#endregion