Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compare strings in Linq Query

CompareTo is not working here for me.

My linq query is

var result = from c in customers 
             where c.CustomerID.CompareTo(txtSerchId.Text) >= 0 
             select` c;

and em getting an exception

//////EXCEPTION///////////

System.ArgumentException was caught
Message=Value does not fall within the expected range.

My code is something like this

var result = 
    from c in customers 
    where c.CustomerID.CompareTo(txtSerchId.Text) >= 0 
    select c;

if (result != null)
{
    IEnumerator<Customer> resultEnum = result.GetEnumerator();
    while (resultEnum.MoveNext())
    {
        Customer c = (Customer)resultEnum.Current;
        addToDataSet(Guid.NewGuid().ToString(), c);
    }
    ShowResult();
}
else
{
    MessageBox.Show("No Customer found within criteria");
}

exception is at this line

IEnumerator<Customer> resultEnum = result.GetEnumerator();
like image 444
MBasit Avatar asked May 04 '12 12:05

MBasit


People also ask

How to compare 2 string in c#?

Equals() method is a method of String class. This method takes two strings to be compared as parameters.

How to ignore case in string compare c#?

ToUpper() Method in C# If we want to compare two string variables by ignoring cases, we can convert both strings to upper-case characters and then compare them. The String. ToUpper() method converts a string to uppercase characters.


1 Answers

try this :

var query = from c in customers where c.CustomerID.Equals(txtSerchId.Text) select c;
like image 100
Ovais Khatri Avatar answered Oct 08 '22 06:10

Ovais Khatri