我试图从“报价”列表中获取随机列表项。我目前有一个webpart,它有一个自定义属性,用于选择要显示的引用类型。 “报价”列表引用了以下类别“公司,技术和财务”。目前我使用的是foreach循环,显示特定类别的所有报价。我有CAML查询来过滤要显示的报价类型。在webpart的custom属性中输入的值在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()));
}
}
}
}
}
我确信有一种简单的方法可以实现这一目标。任何建议都会非常感激。
提前致谢。
答案 0 :(得分:2)
对不起,您发布的代码似乎对格式有点混乱,但我会做以下事情:
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];
}
答案 1 :(得分:1)
你需要
count = collListItems.Count
collListItems[randNumber % count]
答案 2 :(得分:0)
已经成功地将其排除在外。这样做如下:
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));
}
排序!感谢大家的建议。
答案 3 :(得分:-1)
只要将引号划分为正确的类别列表,您只需随机获取并从列表中提取项目即可。
//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];
这很简单粗俗(假设你已经在方法中封装了功能),但它应该为你提供一个基本的指南。我还没有阅读完整的代码示例,因此您需要完成此工作。