字节数组与 NHibernate 的比较
以下 Linq to NHibernate 查询会导致 System.NotSupportedException
。
IEnumerable<File> FindByMd5(byte[] md5)
{
return this.Session.Query<File>().Where(f => f.Md5.SequenceEqual(md5)).ToList();
}
我应该如何使用 Linq to NHibernate 或 QueryOver
The following Linq to NHibernate query results in a System.NotSupportedException
.
IEnumerable<File> FindByMd5(byte[] md5)
{
return this.Session.Query<File>().Where(f => f.Md5.SequenceEqual(md5)).ToList();
}
How should I do this using Linq to NHibernate or QueryOver<File>()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于该错误已经表明 NHibernate 不支持该功能。我将创建一个命名查询并求解查询中的方程。我已经使用 MySQL 对其进行了测试(使用常见的用户名和密码比较作为示例),并且以下语句返回所需的行(密码是 BINARY(32) 字段):
使用 MSSQL 您可以执行以下操作:
因此,将其扩展为命名查询您将创建一个 .hbm.xml 文件,如“User.hbm.xml”,其中包含以下内容:
为了配置它,我使用了 Fluent NHibernate,但仅使用普通的 NHibernate 也可以实现类似的操作:
此语句查找程序集中的“.hbm.xml”文件,其接口名为“IHaveFluentNHibernateMappings”。
有了此文件,您可以在会话级别执行以下操作:
通过调用 GetUserByCredentials 方法,将执行自定义查询。
正如您所看到的,密码是一个字符串,因此您需要首先使用以下命令将 MD5 字节数组转换为字符串:
祝您好运!
Due to the fact that the error is already indicating that NHibernate does not support that feature. I would create a named query and solve the equation within a query. I have tested it with MySQL (using a common username and password comparison as example) and the following statement returns the desired row (password is a BINARY(32) field):
Using MSSQL you can do:
So to extend this to a named query you would create an .hbm.xml file like 'User.hbm.xml' with the following content:
To configure this I used Fluent NHibernate but something similar would be possible with just plain NHibernate:
This statement looks for the ".hbm.xml" files in the assembly with the interface named "IHaveFluentNHibernateMappings"
With this in place you can do the following at session level:
And by calling the the GetUserByCredentials method, the custom query will be executed.
As you can see password is a string, so you need to convert your MD5 byte array to a string first by using:
Good luck!
老Q,但是 Linq 仍然有问题。因此,使用 NHibernate 和 SQLite,当与字节数组值进行比较时,您可以使用限制或条件查询。
或者
Old Q but, still issues with Linq. So, using NHibernate and SQLite, when comparing against a byte array value, you can use query over restrictions or criterias.
or
我通过将 MD5 存储为字符串解决了这个问题。
使用此方法保存文件:
使用此方法检索文件:
I’ve solved the problem by storing the MD5 as a string.
Files are saved using this method:
Files are retrieved using this one: