Payal
Payal

Reputation: 73

Using Trim in multiple where clause of linq with Join clause

One of the columns of my table has spacing in them. I'm trying to query it using linq, but I get an error. The code is:

(from t1 in table1
join t2 in table2
on t1.t2_Id equals t2.Id
where t1.status == "Active"
where t2.column_value.Trim() == parameter
select t1).ToList();

The error is:

Invalid 'where' condition. An entity member is invoking an invalid property or method.

How do I trim the column?

Upvotes: 2

Views: 2311

Answers (2)

Eric J.
Eric J.

Reputation: 150108

The problem is that .Trim() cannot be executed server-side. You can see this from the error

Invalid 'where' condition. An entity member is invoking an invalid property or method.

And that Trim() is the only property or method invoked. I understand that SQL Server should be able to process Trim(), but not every provider can (UPDATE: It is confirmed that the provider is the CRM Linq Provider).

You can use

(from t1 in table1
join t2 in table2
on t1.t2_Id equals t2.Id
where t1.status == "Active"    
select t1).AsEnumerable().Where(r => r.column_value.Trim() == parameter)
.ToList();

AsEnumerable() causes the rest of the Linq expression to be executed client side rather than in the database. One issue with this approach is that you will quite possibly retrieve more records from the database than you end up using, since some filtering is client side.

Upvotes: 2

Arindam Nayak
Arindam Nayak

Reputation: 7462

You need to do like this. To join conditions either you need to use && or || based on your requirement.

from t1 in table1
join t2 in table2
on t1.t2_Id equals t2.Id
where t1.status == "Active"
&& t1.column_value.Trim() == parameter
select t1).ToList();

Upvotes: 3

Related Questions