2012-02-06 170 views
-2

我想從「行情」列表中獲得一個隨機列表項。我目前有一個web部件,它具有用於選擇要顯示的報價類型的自定義屬性。 「行情」列表中引用了以下類別「企業,技術和財務」)。目前,我正在使用一個foreach循環來顯示特定類別的所有引號。我有CAML查詢來過濾要顯示的引號類型。在web部件的自定義屬性中輸入的值用於CAML查詢以顯示引號。生成一個隨機數得到一個隨機列表項

下一步是顯示一個特定類別的隨機引用,但我不太確定如何實現這一點。下面是我的代碼currenlty已經隨機位尚未完成不肯定就如何做到這一點。

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

您嘗試完成代碼時面臨什麼特定問題? – phoog 2012-02-06 18:00:57

+0

您當前的代碼正在處理錯誤地生成隨機數。你的當前代碼會產生一個新的隨機種子,這種方式往往會導致非隨機序列。什麼不適合你的工作,你似乎沒有在本網站上獲得幫助所需的關鍵部分,完全理解你的問題是什麼,只是問一個關於這個問題的問題。你也想創建一個不大於你的收藏大小的數字。 rndQuote.Next()的位置是正確的,你初始化它的位置應該在循環的外部。 – 2012-02-06 18:05:42

+0

@丹尼爾 - 我打算低估這個問題,直到你試圖完全實現你正在嘗試做的事情,並且你完全解釋什麼是和什麼不工作。這將允許您解決一些設計問題(即以不正確的方式初始化隨機類)並試圖獲得問題的全部範圍。 – 2012-02-06 18:07:01

回答

1

你需要

  • 項目的數量,count = collListItems.Count
  • 模操作,collListItems[randNumber % count]
+2

使用Random.Next(int32,int32)並生成0和count之間的數字會不會更容易?這將允許他使用該隨機數的結果作爲索引值? – 2012-02-06 18:09:27

+0

@Rhhound - 稍微容易一點,但我認爲這是更一般和教育。 – 2012-02-06 18:11:18

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

只要你將報價劃分到適當的類別列表中,您可以簡單地獲得一個隨機數並從列表中拉出一個項目。

//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]; 

這是簡單粗暴(和假設你已經封裝在一個方法的功能),但它應該給你一個基本的指路牌。我沒有閱讀完整的代碼示例,因此請根據需要修改此內容以完成工作。

+0

我的理解是,每次你想產生下一個隨機數時,你都不應該對Random對象的實例進行重組,因爲種子值會不斷變化。 – 2012-02-06 18:14:33

+0

Random.Next的上限是非包含的,所以第二行應該讀取'var itemNumber = random.Next(0,technologyList.Count);' – phoog 2012-02-06 18:15:09

+1

@Rhhound不斷創建新的Random的最常見的陷阱因爲種子是基於系統時鐘的,所以你得到了多個具有*相同*種子的實例,並且每個實例返回的第一個數字是相同的。 (循環通常以比系統時鐘週期更高的頻率重複) – phoog 2012-02-06 18:18:50

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)); 
} 

Sorted !.感謝所有的建議。