LINQ Searching Only Allowing Equivalency
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.
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.
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
.
You can do < and > on chars but not on strings ;) Use String.Compare or you can write your own comparer to compare 2 strings.
精彩评论