如何比较List<>带有文本框的值

时间:2014-03-27 10:36:32

标签: c# list

我从列表CheckProduct(int id)中的发票表中获取产品ID:

int id = int.Parse(invoice_no.Text);  // passing textbox value to list
SPBusinesslogic ab = new SPBusinesslogic();
List<SPBusinesslogic> invv = new List<SPBusinesslogic>();
invv = ab.CheckProduct(id);

 if (invv.Equals(int.Parse(up_invno_txtbox.Text)))  /comparing values in list
    {
        MessageBox.Show("the product already exist");
    }
    else
    {  //add new product code
    }

我无法通过此比较价值 如果我犯了任何错误,请告诉我。

CheckProduct:

public List<SPBusinesslogic> CheckProduct(int id)
{
    try
    {
        SPDatalogic sp = new SPDatalogic();
        DataTable dt = new DataTable();
        dt = sp.CheckProduct(id);
        List<SPBusinesslogic> invinfo = new List<SPBusinesslogic>();
        foreach (DataRow dr in dt.Rows)
        {
            SPBusinesslogic ab = new SPBusinesslogic();
            ab.Pro_id = int.Parse(dr[0].ToString());

            invinfo.Add(ab);
        }
        return invinfo;
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }

3 个答案:

答案 0 :(得分:4)

我怀疑你想要 之类的东西:

// TODO: Give your variables more meaningful names throughout...
int id = int.Parse(up_invno_txtbox.Text);
if (invv.Any(item => item.Id == id))
{
    ...
}
else
{
    ...
}

显然根据您的要求调整item的使用,但这种一般模式适合于查找谓词的现有匹配。

答案 1 :(得分:2)

如果您在列表中匹配的参数是id,则这应该适用于比较检查

(invv.Where(a=> a.id == int.Parse(up_invno_txtbox.Text)).Count() > 0) 

甚至

(invv.Where(a=> a.id == int.Parse(up_invno_txtbox.Text)).FirstOrDefault() != null)

答案 2 :(得分:0)

if(invv.Any(o=>o.id==Convert.ToInt32(up_invno_txtbox.Text)))
{
     MessageBox.Show("the product already exist");
}
else
{
    //add new product code

}