将Rank转换为C#百分比

时间:2014-08-01 18:07:53

标签: c# asp.net

我正在尝试根据搜索中返回的每条记录的排名来显示百分比。我希望它循环遍历每个项目,但它只循环通过第一个项目多次,因为我有项目。例如,如果它找到4个结果,它将显示所有4个结果中第一个的排名。

是否有任何建议让它分别显示每个等级并将其转换为百分比?

private void BindRpt()
{
    if (string.IsNullOrEmpty(txtSearch.Text)) return;
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
    cn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    cmd.CommandText = "select Distinct Rank, SUBSTRING(ColumnA, 1, 500) AS ColumnA, ColumnB, ColumnC, ColumnD, ColumnE from FREETEXTTABLE (TABLE , ColumnA, '" + Search.Text + "'  ) S, TABLE C WHERE c.ID = S.[KEY] order by Rank Desc";

    DataTable dt = new DataTable();
    adapter.SelectCommand = cmd;
    adapter.Fill(dt);

    PagedDataSource pgitems = new PagedDataSource();
    pgitems.DataSource = dt.DefaultView;
    pgitems.AllowPaging = true;

    pgitems.PageSize = 3;
    pgitems.CurrentPageIndex = PageNumber;
    if (pgitems.Count > 1)
    {                
        rptPaging.Visible = true;
        ArrayList pages = new ArrayList();
        for (int i = 0; i <= pgitems.PageCount - 1; i++)
        {
            pages.Add((i + 1).ToString());
        }
        rptPaging.DataSource = pages;
        rptPaging.DataBind();

        lblSentence.Visible = true;
        lblSearchWord.Visible = true;
        lblSearchWord.Text = txtSearch.Text;
    }
    else 
    {
        rptPaging.Visible = false;

        lblSentence.Visible = true;
        lblSentence.Text = "Results were found for";

        lblSearchWord.Visible = true;
        lblSearchWord.Text = txtSearch.Text;
    }
    rptResults.DataSource = pgitems;
    rptResults.DataBind();
    cn.Close();
}

protected void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["HTAA"].ConnectionString);
    cn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    cmd.CommandText = "select Distinct Rank, SUBSTRING(ColumnA, 1, 500) AS ColumnA, ColumnB, ColumnC, ColumnD, ColumnE from FREETEXTTABLE (TABLE , ColumnA, '" + Search.Text + "'  ) S, TABLE C WHERE c.ID = S.[KEY] order by Rank Desc";


    int number = Page.Items.Count;
    SqlDataReader dr = cmd.ExecuteReader();
    if(dr.Read())
    {
        int firstrank = dr.GetInt32(0);
        while (dr.Read())
        {
            int rank = dr.GetInt32(0);
            int percentage = (rank / firstrank) * 100;
            Label lblpre = (Label)e.Item.FindControl("lblRank");
            lblpre.Text = percentage.ToString();
        }
    }
    dr.Close();
    cn.Close();
}

4 个答案:

答案 0 :(得分:1)

聊天之后,我对事情有了更好的处理。一种方法;

在代码隐藏文件后创建一个私有字段。

private int topRanked = 0; 

在你的Bind方法()

private void Bind() 
 { 
 ... 
 DataTable dt = new DataTable(); 
 adapter.SelectCommand = cmd; 
 adapter.Fill(dt); 

 topRanked = (int)dt.Rows[0]["Rank"];

现在,制作OnItemDataBound方法;

protected void OnItemDataBound(object sender, RepeaterItemEventArgs e) 
 { 
 var dataItem = e.Item.DataItem as DataRowView; 
 int rank = (int) dataItem["Rank"]; 

 var percentage = ((double)topRanked / rank) * 100; 

 Label label = (Label)e.Item.FindControl("labelRank"); 

 label.Text = percentage.ToString(); 
 }

如上所述。我不相信这是最好的答案,但这是一个答案。我确定一个存储过程,甚至更好的sql方法都可能计算出来,而不是让你在代码中进行计算。

答案 1 :(得分:0)

您可以尝试使用while(dr.Read())而不是“if”吗?

答案 2 :(得分:0)

您需要循环结果集

while (dr.Read())
{
   int rank = dr.GetInt32(0);
   int percentage = (rank / rank) * 100;
   Label lblpre = (Label)e.Item.FindControl("lblRank");
   lblpre.Text = rank.ToString();

}

答案 3 :(得分:0)

&#34;但它只会循环显示第一个项目&#34; - 因为您有for并检查i <= 0

for (int i = 0; i <= 0; i++) 
{
   ....
}

您不需要此for声明,而是使用

if (dr != null)
   using (dr)
   {
         while (dr.Read())
         {
          ..
         }
   }

在处理数据库连接对象时,最好使用using,以便在使用这些对象后使用的资源得到妥善处理。