Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回包含一个随机项的SPListItemCollection_C#_Sharepoint - Fatal编程技术网

C# 返回包含一个随机项的SPListItemCollection

C# 返回包含一个随机项的SPListItemCollection,c#,sharepoint,C#,Sharepoint,我有一个SplitItemCollection 我基本上想得到一个项目(随机)在收集和返回它 我的方法有一个需要SPListItemCollection的返回类型,所以我不介意让SPListItemCollection只包含一个项并返回它 我不知道如何返回包含这一项的SPListItemCollection 我该怎么做 在集合中选择一个随机项。为Rand对象设定种子,然后在从中获取随机数时,使用数组的边界定义随机对象的边界(0,spListItemCollection.Count) 取下该物品,

我有一个SplitItemCollection

我基本上想得到一个项目(随机)在收集和返回它

我的方法有一个需要SPListItemCollection的返回类型,所以我不介意让SPListItemCollection只包含一个项并返回它

我不知道如何返回包含这一项的SPListItemCollection


我该怎么做

在集合中选择一个随机项。为Rand对象设定种子,然后在从中获取随机数时,使用数组的边界定义随机对象的边界(0,spListItemCollection.Count)


取下该物品,将其包装在新的SPListItemCollection中并返回

我认为您必须将项目添加到要返回的集合中,然后必须调用update()

然后您可以返回CollListItemsTest


但实际上,你应该考虑只归还物品,而不是收藏品。如果您总是知道在重读时只会有一个项目,那么返回集合就没有意义了。看起来您希望将SPListItemCollection减少为单个项目,但有一种方法可以直接从列表中获取带有单个随机项目的SPListItemCollection:

private SPListItemCollection GetRandomItem(SPList list)
{
    Random random = new Random();
    int index = random.Next(0, list.ItemCount - 1);
    SPQuery query = new SPQuery();
    query.Query = string.Format("<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Integer\">{0}</Value></Eq></Where>", index);
    return list.GetItems(query);
}
private SPListItemCollection GetRandomItem(SPList列表)
{
随机=新随机();
int index=random.Next(0,list.ItemCount-1);
SPQuery query=新建SPQuery();
query.query=string.Format(“{0}”,索引);
返回列表.GetItems(查询);
}

是将项目包装到一个新的SPListItemCollection中导致了这个问题。我随机得到了这个项目等等。。。但是不知道如何做下一部分你不能创建一个新的SpListItemCollection这应该是最好的答案。只需使用随机数来获取项目ID,并使用上述方法来获取您想要的结果“FieldRef”ID始终从0到计数?
private SPListItemCollection GetRandomItem(SPList list)
{
    Random random = new Random();
    int index = random.Next(0, list.ItemCount - 1);
    SPQuery query = new SPQuery();
    query.Query = string.Format("<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Integer\">{0}</Value></Eq></Where>", index);
    return list.GetItems(query);
}