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;

最佳答案

我假设您不想修改存储在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() ;
}

09-08 07:47