2013-07-08 66 views
2
int length3 = CheckBoxList4.Items.Count; 
int count = 0;   
for (int i = 0; i < length3; i++) 
{ 
    BooleanQuery finalQuery1 = new BooleanQuery();     
    finalQuery1 = (BooleanQuery)Session["Luc_Query"]; 
    var query1 = new QueryParser("Industry", analyzer).Parse(CheckBoxList4.Items[i].Text); 
    finalQuery1.Add(query1, BooleanClause.Occur.MUST);     
    hits = searcher.Search(finalQuery1); 
    count = hits.Length(); 
    CheckBoxList4.Items[i].Text = CheckBoxList4.Items[i].Text +" " +count.ToString() ; 
} 

我試圖null爲finalQuery1,但它重複了這個query(finalQuery1.Add(query1, BooleanClause.Occur.MUST);)的增值。我用於null BooleanQuery的方法在下面,但它仍然不起作用。如何清空lucene.net中的BooleanQuery對象?

finalQuery1 = (BooleanQuery)null; 
finalQuery1=null; 

回答

1

我假設你不希望修改存儲在該Session["Luc_Query"]查詢,只要有它在每次循環不變。

您的代碼不起作用,因爲您在循環的每次迭代中都修改了查詢的同一個實例。您需要對原始查詢的副本進行更改,而不是更改原始查詢。

嘗試下面的代碼:

int length3 = CheckBoxList4.Items.Count; 
int count = 0;   
for (int i = 0; i < length3; i++) 
{ 
    BooleanQuery finalQuery1 = (BooleanQuery)Session["Luc_Query"]; 
    finalQuery1 = (BooleanQuery)finalQuery1.Clone(); 
    var query1 = new QueryParser("Industry", analyzer).Parse(CheckBoxList4.Items[i].Text); 
    finalQuery1.Add(query1, BooleanClause.Occur.MUST);     
    hits = searcher.Search(finalQuery1); 
    count = hits.Length(); 
    CheckBoxList4.Items[i].Text = CheckBoxList4.Items[i].Text + " " + count.ToString() ; 
} 
+0

缺少強制。答案已更新。 –

+0

謝謝@馬拉斯Musielak.works偉大 –