生成一个随机数以获得一个随机列表项

本文关键字:一个 随机 列表 随机数 | 更新日期: 2023-09-27 18:22:37

我正试图从"报价"列表中获取一个随机列表项。我目前有一个Web部件,它有一个自定义属性,用于选择要显示的报价类型。"报价"列表包含以下类别的报价(企业、技术和金融)。目前,我使用foreach循环来显示特定类别的所有报价。我使用CAML查询来筛选要显示的报价类型。在Web部件的自定义属性中输入的值在CAML查询中用于显示报价。

下一步是只显示一个特定类别的随机引用,但我不太确定如何实现这一点。下面是我目前拥有的代码,随机位还没有完成,因为不确定如何做。

protected void Page_Load(object sender, EventArgs e)
{
if (this.WebPart != null && this.WebPart.PracticeArea != null)
{
string PracticeArea = this.WebPart.PracticeArea; //get the value of the property

//getting a reference to the site location
string webUrl = SPContext.Current.Site.AllWebs["practices"].Url;
//Getting the Quotes list
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
//getting the Quotes list
SPList quotesList = web.Lists["Quotes"];
//SPListItemCollection collLisItems = quotesList.Items; not needed
//CAML query to filter or obtain the correct quote based on Area. Value for Area is 
//passed from the custom property and used in the caml query
SPQuery quotesbySector = new SPQuery();
//creating an object to handle our random list item selection
//not too sure whether this is correct
Random rndQuote = new Random();
int num = rndQuote.Next();

//string camlquery1 = "<Where><Eq>" + "<FieldRef Name='Area'/>" + "</Eq></Where>";
string camlquery1 = @"
<Where>
<Eq>
<FieldRef Name='Area'/>
<Value Type='Text'>" + PracticeArea + @" </Value>
</Eq>
</Where>";
quotesbySector.Query = camlquery1;
SPListItemCollection collListItems = quotesList.GetItems(quotesbySector);
//SPListItem firstQuote = collListItems[0];
//for each loop might need to be removed, as we are only interested in getting a 
//random quote and not all quotes
foreach (SPListItem item in collListItems)
{
string quotes = (string)item["Quote"];
string quotesSource = (string)item["Source"];
string quotesYear = (string)item["Year"];
//string quotesArea = (string)item["Area"]; //not needed used for test purposes
plhQuotes.Controls.Add(new LiteralControl(quotes + "<br/>" + "<br/>" + quotesSource + 
"<br/>" + "<br/>" + quotesYear + "<br/>" + "<br/>"));
}

}
catch (Exception err)
{
plhErrors.Controls.Add(new LiteralControl(err.ToString()));
}
}
}



}
}

我相信有一个简单的方法可以实现这一点。任何建议都将不胜感激。

提前谢谢。

生成一个随机数以获得一个随机列表项

很抱歉你发布的代码在格式上有点混乱,但我会做以下操作:

List<String> quoteList = new List<String>();
Random rand = new Random();
String quote;
if(quoteList.Count > 0)
{
  int index = rand.Next(quoteList.Count); // Returns 0 through number of items minus 1
  quote = quoteList[index];
}

您需要

  • 项目数,count = collListItems.Count
  • 模运算collListItems[randNumber % count]

已经很简单地解决了这个问题。操作如下:

SPListItemCollection collListItems = quotesList.GetItems(quotesbySector);
//Getting a random list item from based on the PracticeArea value entered in webpart's 
//custom property, which is used in the CAML query above to filter the results returned
Random randomQuote = new Random();
int randItem = randomQuote.Next(0, collListItems.Count);
collListItems[randItem].ToString();
SPListItem ourQuote = collListItems[randItem];
string quote = (string)ourQuote["Quote"];
string quoteSource = (string)ourQuote["Source"];
string quoteYear = (string)ourQuote["Year"];
if (quoteYear != null)
{
plhQuotes.Controls.Add(new LiteralControl(quote + "<br/><br/>" + quoteSource + "<br/>
<br/>" + "Year: " + quoteYear));
}
else
{
plhQuotes.Controls.Add(new LiteralControl(quote + "<br/><br/>" + quoteSource));
}

排序!。感谢大家的建议。

只要将引号划分到适当的类别列表中,就可以简单地获得一个随机项并从列表中提取一个项。

//Assuming individual lists for categories (ex: technologyList)
//for technology list
Random random = new Random(seed); //seed is optional
var itemNumber = random.Next(0, technologyList.Count);
return technologyList[itemNumber];

这既简单又粗糙(假设您已经将功能封装在一个方法中),但它应该为您提供一个基本的指南。我还没有阅读完整的代码示例,所以根据需要修改它以完成您的工作。