String.IsNullOrWhiteSpace在LINQ防爆pression [英] String.IsNullOrWhiteSpace in LINQ Expression

查看:298
本文介绍了String.IsNullOrWhiteSpace在LINQ防爆pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

return this.ObjectContext.BranchCostDetails.Where(
    b => b.TarrifId == tariffId && b.Diameter == diameter
        || (b.TarrifId==tariffId && !string.IsNullOrWhiteSpace(b.Diameter))
        || (!b.TarrifId.HasValue) && b.Diameter==diameter);

和我得到这个错误,当我尝试运行code:

And I get this error when I try to run the code:

LINQ到实体无​​法识别方法'布尔
  IsNullOrWhiteSpace(System.String)'方法,和这种方法不能
  翻译成一家商店前pression。

LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression."

我怎样才能解决这个问题,写code比这更好的?

How can I solve this problem and write code better than this?

推荐答案

您需要更换

!string.IsNullOrWhiteSpace(b.Diameter)

!(b.Diameter == null || b.Diameter.Trim() == string.Empty)

有关LINQ到实体这个被翻译成:

For Linq to Entities this gets translated into:

DECLARE @p0 VarChar(1000) = ''
...
WHERE NOT (([t0].[Diameter] IS NULL) OR (LTRIM(RTRIM([t0].[Diameter])) = @p0))

和LINQ to SQL的几乎,但不完全一样。

and for Linq to SQL almost but not quite the same

DECLARE @p0 NVarChar(1000) = ''
...
WHERE NOT (LTRIM(RTRIM([t0].[TypeName])) = @p0)

这篇关于String.IsNullOrWhiteSpace在LINQ防爆pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆