views:

40

answers:

3

Hi folks, I'm trying to filter a set of records based on a sub-object's criteria. This compiles ok

recordList = recordList.Where(r => r.Areas.Where(a => a.Area == "Z").Count() > 0);

but this doesn't

recordList = recordList.Where(r => r.Areas.Where(a => a.Area <= "Z").Count() > 0);

giving these errors Cannot convert lambda expression to type 'string' because it is not a delegate type Delegate 'System.Func' does not take '1' arguments Operator '<=' cannot be applied to operands of type 'string' and 'string'

!= works ok, by any sort of less than or greater than operation fails.

+1  A: 

Operator '<=' cannot be applied to operands of type 'string' and 'string'

Well there it is. To compare for ordering, rather than for equality, use string.Compare.

AakashM
+1  A: 

You can do < and > on chars but not on strings ;) Use String.Compare or you can write your own comparer to compare 2 strings.

m0s
+2  A: 

This has got nothing to do with LINQ not allowing the use of <=, but that strings cannot be compared with this operator (yet can obviously use equal or not equal).

Writing this in a console app:

string strA = "jamie";
string strB = "Z";
Console.WriteLine(strA != strB);
Console.WriteLine((bool)(strA <= strB));

Underlines the final line with (basically) the same error

Cannot apply operator '<=' with type 'string' and 'string'

Now, you can use the <= operator on char, so the similar code below compiles and works as expected:

char charA = 'J';
char charB = 'Z';
Console.WriteLine(charA<=charB);

So maybe what you want is:

recordList = recordList.Where(r => r.Areas.Where(a => a.Area[0] <= 'Z').Count() > 0);

HTH.

Jamiec
Thanks folks - could have sworn I've done that with string elsewhere, which is why I asked the question, but maybe I haven't....(Might be thinking of when I was doing this with the dynamic SQL class)
Mad Halfling
@Mad : T-SQL, VB classic, VB.NET all allow the use of `<` etc as operators on string values. It's C# that's different here.
AakashM