为什么 Trim() 在这个表达式中失败?
我有以下方法:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
这不会返回任何内容,因为在数据库中该字段是nchar(10)
。我想要 Trim()
TypeCode 值,如下所示:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode.Trim() == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
但它返回 NHibernate 错误:
Unrecognised method call in epression c.TypeCode.Trim()
办公室里的一个人认为这是因为 HHibernate 不知道如何转换 。 Trim()
到 SQL(或类似的东西)。谁能建议我如何解决这个问题?
I have the following method:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
This is returning nothing because in the database the field is nchar(10)
. I want to Trim()
the TypeCode value, as follows:
var catIds = DetachedCriteria.For<Category>()
.Add<Category>(c => c.TypeCode.Trim() == "IMA")
.SetProjection(LambdaProjection.Property<Category>(s => s.Id));
but it returns the NHibernate error:
Unrecognised method call in epression c.TypeCode.Trim()
One of the guys here in the office thinks it's because HHibernate doesn't know how to convert .Trim()
to SQL (or something along those lines). Can anyone suggest how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将要比较的值右填充到所需的长度,例如:
Try right-padding the value you're comparing with to the required length, for example:
您的办公室人员是正确的 - linq 提供程序不知道如何将 C# string.Trim() 转换为任何 sql 变体。
至于修复,linq 可以让你做最该死的事情——比如填充数据以匹配 CHAR(10) 而不是 TRIM() 到“正常”字符串。
Your office guys are correct -- the linq provider doesn't know how to translate C# string.Trim() to whatever sql variant it is.
As for the fix, linq can make you do the damndest things -- like padding your data to match CHAR(10) rather than TRIM() to a 'normal' string.
我希望下面的代码可以解决您的问题:
然后像这样使用它:
I hope the following code may solve your problem:
and then use it like: