LINQ 表达式中的 String.IsNullOrWhiteSpace [英] String.IsNullOrWhiteSpace in LINQ Expression

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

问题描述

我有以下代码:

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

当我尝试运行代码时出现此错误:

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

LINQ to Entities 无法识别方法 'BooleanIsNullOrWhiteSpace(System.String)'方法,该方法不能翻译成商店表达."

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

我怎样才能解决这个问题并写出比这更好的代码?

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 to Entities,这被翻译成:

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)

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

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