Bhargav Amin
Bhargav Amin

Reputation: 1177

How to Compare List<> values with a Textbox

i am fetching product id from invoice table in a list CheckProduct(int 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
    }

i am unable to compare the value by this let me know if i am committing any mistake.

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

Upvotes: 0

Views: 1620

Answers (3)

Subhash Rao
Subhash Rao

Reputation: 335

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

}

Upvotes: 0

Hano Johannes Rossouw
Hano Johannes Rossouw

Reputation: 770

If the param to which you match in your list is id this should work for the compare check

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

or even

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500805

I suspect you want something like:

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

Obviously adjust the use of item as per your requirements, but this general pattern is suitable for finding an existing match for a predicate.

Upvotes: 4

Related Questions