10

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();
rene
  • 41,474
  • 78
  • 114
  • 152
MBasit
  • 101
  • 1
  • 1
  • 4
  • what is the value in `txtSerchId.Text` and what is the expected output? You are comparing `CustomerID` to an arbitrary string???? – Bazzz May 04 '12 at 12:05
  • txtSerchId is TextFeild(WindowForm Contorl) i am comparing user entered value to collection of objects i have, to search Customers having IDs less than or you can say greater than that entered by user. – MBasit May 04 '12 at 12:11
  • and it's `Text` is? And what sense has it to compare this text to a `CustomerID`? Maybe I'm just not understanding your business requirement. – Bazzz May 04 '12 at 12:13
  • `if (result != null)` is never `null`. You should use `result.Any()` to check whether or not the sequence contains any elements. – Tim Schmelter May 04 '12 at 12:15
  • yess! it is text even if some1 enter some numeric in textfeild, Textfeild's Text property will manipulate that as text. – MBasit May 04 '12 at 12:15
  • OOhhhhh! it gives same exception when i call result.Any() can some1 figure it out – MBasit May 04 '12 at 12:25

4 Answers4

11

try this :

var query = from c in customers where c.CustomerID.Equals(txtSerchId.Text) select c;
Ovais Khatri
  • 3,201
  • 16
  • 14
  • 2
    I will suggest String.Equals (objA,objB) instead of objA.Equals(objB). – Tilak May 04 '12 at 12:09
  • yah! i was just to mention that when i use Equals method(that is not requirement just for check) that works great, and this is more frustrating for me........ – MBasit May 04 '12 at 12:18
0

Quoting from your comment "i am comparing user entered value to collection of objects i have, to search Customers having IDs less than or you can say greater than that entered by user."

try this for "greater than":

int customerId = int.Parse(txtSerchId.Text);
if (customerId > 0)
{
   var result = from c in customers where c.CustomerID > customerId select c;
}

Update as more infomation has been added in comments:

Try this:

customers.ToList().Where(c => c.CustomerID.CompareTo(txtSerchId.Text) >= 0);

Notice that this is vastly inefficient as it first pulls ALL records from the database and THEN filters them according to your string comparison. But to be honest I don't know a better way so this might be worth a try.

Bazzz
  • 26,427
  • 12
  • 52
  • 69
  • Interesting, then you do you decide whether a certain CustomerID is greater than another? – Bazzz May 04 '12 at 12:55
  • Exactly! greater on the basis of alphabetic order, this is what CompareTo method does simply But it is not working in LINQ contest Do not know Y??? CompareTo method of string class takes one string argument and compare it with 'this'(calling) object returns as follow: 0 if equal -1 if argument is greater e..g ("c".CompareTo("d")) 1 if argument is less e..g ("c".CompareTo("a")) – MBasit May 04 '12 at 13:47
  • probably because there is no translation for "Compare" to SQL, so the IQueryable cannot handle it. You could pull the whole set into a List and then use Compare to get the records that you need. This is not a very efficient way and might induce a performance hit as it needs to pull every record, but it's worth a try: `customers.ToList().Where(c.CustomerID.CompareTo(txtSerchId.Text) >= 0);` – Bazzz May 07 '12 at 06:41
  • use of LINQ is mandatory in my scenario how can i integrate this solution in lINQ – MBasit May 07 '12 at 13:27
0

Go simple:

  1. For Equality:

    var result = from c in customers where c.CustomerID ==Convert.ToInt32(txtSerchId.Text) select c;

  2. For Greater : where c.CustomerID >= Convert.ToInt32(txtSerchId.Text)

  3. For less : where c.CustomerID <= Convert.ToInt32(txtSerchId.Text)

0
 var List = (from t in ObjCon.TableName
                            where t.GameDate.Value.CompareTo(GameDate) >= 0
                            join t1 in ObjCon.Teams on t.Home equals t1.TeamId
                            where t1.SportId == 3

*This Worked FOR ME