简体   繁体   English

在带有Join子句的linq的多个where子句中使用Trim

[英]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. 我正在尝试使用linq查询它,但出现错误。 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. 无效的“ where”条件。 An entity member is invoking an invalid property or method. 实体成员正在调用无效的属性或方法。

How do I trim the column? 如何修剪色谱柱?

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();

The problem is that .Trim() cannot be executed server-side. 问题是.Trim()无法在服务器端执行。 You can see this from the error 您可以从错误中看到这一点

Invalid 'where' condition. 无效的“ where”条件。 An entity member is invoking an invalid property or method. 实体成员正在调用无效的属性或方法。

And that Trim() is the only property or method invoked. 而且Trim()是唯一调用的属性或方法。 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 ). 我了解SQL Server应该能够处理Trim(),但不是每个提供程序都可以( 更新:已确认该提供程序为CRM Linq提供程序 )。

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. AsEnumerable()使Linq表达式的其余部分在客户端而不是数据库中执行。 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. 这种方法的一个问题是,由于某些过滤是在客户端进行的,因此您从数据库中检索到的记录可能要多于最终使用的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM