比较不能比较c#

本文关键字:比较 不能 | 更新日期: 2023-09-27 18:16:09

我在比较用户输入的值和数据库时遇到了一个问题。

这是变量

List <string> onewayorigin = new List<string>();
List <string> onewaydestination = new List<string>();      
List <string> onewayterminal = new List<string>();
List <string> onewaydeparture = new List<string>();

这里是我连接到数据库来获取值。

string strSelect;
SqlCommand cmdSelect;
SqlDataReader dtr;

SqlConnection conCust;
string connStr = ConfigurationManager.ConnectionStrings["BusConnectionString"].ConnectionString;
conCust = new SqlConnection(connStr);
strSelect = "Select * From Route";
cmdSelect = new SqlCommand(strSelect, conCust);
conCust.Open();
dtr = cmdSelect.ExecuteReader();
while (dtr.Read())
{
    onewayorigin.Add(dtr["OneWayOrigin"].ToString());
    onewaydestination.Add(dtr["OneWayDestination"].ToString());            
    onewayterminal.Add(dtr["OneWayTerminal"].ToString());   
    onewaydeparture.Add(dtr["OneWayDepartureTime"].ToString());            
}
string[] origin = onewayorigin.ToArray();
string[] destination = onewaydestination.ToArray();
string[] ow_terminal = onewayterminal.ToArray();
string[] ow_depart = onewaydeparture.ToArray();

我在这里做比较。

if (ow_terminal.Equals(ddlterminal.SelectedValue) && ow_depart.Equals(ddlDeparture.SelectedValue))
{
    if (origin.Equals(ddlOrigin.SelectedValue) && destination.Equals(ddlDestination.SelectedValue))
    {  
        lblMessage.Text = "Record exist";
    }                
}
else
{
    lblMessage.Text = "No record exist";
}

是的,我承认我是新来的。

比较不能比较c#

问题是您正在比较数组和字符串。结果总是得到false

Contains代替。如下所示:

if (ow_terminal.Contains(ddlterminal.SelectedValue) && ow_depart.Contains(ddlDeparture.SelectedValue))
{
    if (origin.Contains(ddlOrigin.SelectedValue) && destination.Contains(ddlDestination.SelectedValue))
    {
        lblMessage.Text = "Record exist";
    }
}
else
{
    lblMessage.Text = "No record exist";
}

您的比较失败,因为您正在比较一个完整的数组,只有一个值:

ow_terminal.Equals(ddlterminal.SelectedValue)
  ^ this is an array                 ^ this is properly a string

要解决这个问题,您需要循环遍历数组以确保元素(在数组中)与ddlterminal.SelectedValue

中的值匹配。

的例子:

foreach(string terminal in ow_terminal)
{ 
  if(terminal == ddlterminal.SelectedValue)
  { 
    ...

然而,这可以更容易地完成Contains方法:

if(ow_terminal.Contains(ddlterminal.SelectedValue) && ...)

一旦添加到List中,您需要为项目提供索引。因此,在比较的时候,你应该考虑到项目索引,以便你比较相应的值。你可以使用ArrayList class

ArrayList  onewayorigin = new ArrayList() ;
ArrayList  onewaydestination = new  ArrayList() ; 
ArrayList  onewayterminal = new ArrayList() ;
ArrayList  onewaydeparture = new ArrayList() ;
string strSelect;
SqlCommand cmdSelect;
SqlDataReader dtr;
SqlConnection conCust;
string connStr = ConfigurationManager.ConnectionStrings["BusConnectionString"].ConnectionString;
conCust = new SqlConnection(connStr);
strSelect = "Select * From Route";
cmdSelect = new SqlCommand(strSelect, conCust);
conCust.Open();
dtr = cmdSelect.ExecuteReader();
while (dtr.Read())
{
    i = 0;
    onewayorigin.Insert(i, dtr["OneWayOrigin"].ToString());
    onewaydestination.Insert(i, dtr["OneWayDestination"].ToString());            
    onewayterminal.Insert(i, dtr["OneWayTerminal"].ToString());   
    onewaydeparture.Insert(i, dtr["OneWayDepartureTime"].ToString());   
    i++         
}
//to compare
for (int iIndex = 0; iIndex < arr.Count; iIndex++)
{
    object o = arr[iIndex];
    if (ow_terminal[iIndex].ToString().Contains(ddlterminal.SelectedValue) &&    ow_depart[iIndex].ToString().Contains(ddlDeparture.SelectedValue))
    {
        if (onewayorigin[iIndex].ToString().Contains(ddlOrigin.SelectedValue) && destination[iIndex].ToString().Contains(ddlDestination.SelectedValue))
        {
            lblMessage.Text = "Record exist";
            return;
        }
        else
        {
            lblMessage.Text = "No record exist";
        }
    }
    else
    {
        lblMessage.Text = "No record exist";
    }
}