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

我试图使 finalQuery1 为空,但它重复了这个的附加值。query(finalQuery1.Add(query1, BooleanClause.Occur.MUST);)我用来使 BooleanQuery 为空的方法在下面,但它仍然无法正常工作。

finalQuery1 = (BooleanQuery)null;
finalQuery1=null;
4

1 回答 1

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() ;
}
于 2013-07-08T13:23:51.407 回答