简体   繁体   中英

How to compare an alphanumeric string with a List<string> which also has alphanumeric values C#

I'm unable to figure out how to compare an alphanumeric string with a list string which also has alpanumeric values in it.

for example :-

I have an invoice no : Sales111 to check whether the same invoice exist in database I have written a code for that

Here CheckInvoice() return all invoice no from datatable

 public List<String> CheckInvoice()
        {
            try
            {
                SPDatalogic sp = new SPDatalogic(); //datalogic class
                DataTable dt = new DataTable() ;
                dt = sp.CheckInvoice();
                List<String> list = new List<String>();

                foreach (DataRow dr in dt.Rows)
                {                    
                    list.Add(dr["invoice_id"].ToString());// column name of invoice no
                }

                return list;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

Presentation Layer :-

             SPBusinesslogic ab = new SPBusinesslogic(); //businesslogic class
             List<String> inv = new List<String>();
             inv = ab.CheckInvoice();

             if (inv.Any(x => x.Equals(invoiceid_textbox.Text.Trim())))
             {
                        MessageBox.Show("The invoice already exist!");
                    }

What are the ways I can check? How can I do it without sorting alphabets and numericals?

Why you want use Linq for this simple task? You can simply use List<T>.Contains(item) method

        List<string> lollipop = new List<string>();
        //Do something
        if(lollipop.Contains("lollipop"))
         //DO something
        else
        //Something else

It's more simple and for your problem it's perfect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM