Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
LinQ expression not supported in the Where clause
My code accesses a SQL Server database using DevExpress XPO. It contains the following query:
var relevantAccount = accounts
.Where(acc => String.Compare(acc.Description, "Some Account Name", true) == 0)
.FirstOrDefault()
;
I use String.Compare rather than == so I can do a case-insensitive comparison. It compiles, but throws an Exception during runtime:
System.NotSupportedException: 'The expression is not supported in the Where clause: Compare(acc.Description, "Some Account Name", True)'
Why does this Where clause throw an Exception, and how do I fix it?
1 answer
The following users marked this post as Works for me:
| User | Comment | Date |
|---|---|---|
| FractionalRadix | (no comment) | Mar 4, 2026 at 10:46 |
Since the program uses XPO, the system tries to translate this query to SQL. However, the LinQ provider (LinQ-to-XPO) does not support String.Compare. You'd likely have the same problem with other LinQ providers.
It does support ToLower() so a simple solution is:
var relevantAccount = accounts
.Where(acc => acc.Description.ToLower() == "some account name")
.FirstOrDefault()
;
For more complex expressions, it may be necessary to force evaluation in memory using AsEnumerable():
var relevantAccount = accounts
.AsEnumerable()
.Where(acc => ... complex expression over acc... )
.FirstOrDefault()
;

1 comment thread