我有一个看起来像这样的nhibernate linq查询:from b in session.QueryBookmark()where b.Uri.Equals(uri) ||b.Uri.Equals(www. + uri) ||string.Concat(www., b.Uri).Equals(uri)select b这个爆炸,说康卡特...
我有一个看起来像这样的nhibernate linq查询:
from b in session.Query<Bookmark>()
where b.Uri.Equals(uri) ||
b.Uri.Equals("www." + uri) ||
string.Concat("www.", b.Uri).Equals(uri)
select b
这个爆炸,说康卡特不支持,但当我改变它
from b in session.Query<Bookmark>()
where b.Uri.Equals(uri) ||
b.Uri.Equals("www." + uri) ||
("www." + b.Uri).Equals(uri)
select b
它运行正常,但查询如下所示:
select cast(count(*) as SIGNED) as col_0_0_
from bookmarks bookmark0_
where bookmark0_.Uri = 'www.google.com'
or bookmark0_.Uri = 'www.www.google.com'
or 'www.'+bookmark0_.Uri = 'www.google.com';
哪里’www.’ bookmark0_.Uri是“添加”而不是concat(‘www.’,bookmark0_.Uri).有没有办法在Linq for NHibernate for MySQL中连接字符串?
解决方法:
以下是解决此问题的HqlGenerator:
public class ConcatHqlGenerator : BaseHqlGeneratorForMethod
{
public ConcatHqlGenerator()
: base()
{
this.SupportedMethods = new[]
{ ReflectionHelper.GetMethodDefinition(() => string.Concat(null, null)) };
}
public override HqlTreeNode BuildHql(MethodInfo method,
Expression targetObject,
ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder,
IHqlExpressionVisitor visitor)
{
return treeBuilder.Concat(
new[]
{
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression()
});
}
}
将它添加到您的HQLGeneratorsRegistry中,您可以在LINQ语句中调用string.Concat.
public class LinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
public LinqToHqlGeneratorsRegistry()
: base()
{
this.Merge(new ConcatHqlGenerator());
}
}
private static ISessionFactory CreateSessionFactory()
{
var configuration = new NHib.Cfg.Configuration();
configuration.Properties.Add(NHibernate.Cfg
.Environment.LinqToHqlGeneratorsRegistry,
typeof(LinqToHqlGeneratorsRegistry).AssemblyQualifiedName);
configuration.Configure();
return configuration.BuildSessionFactory();
}
沃梦达教程
本文标题为:c# – NHibernate / MySQL字符串连接
基础教程推荐
猜你喜欢
- C# XML中的转义字符操作 2023-03-29
- C#导入和导出CSV文件 2023-06-09
- C# 模拟浏览器并自动操作的实例代码 2023-03-04
- 基于C#实现简易的键盘记录器 2023-06-28
- 带你一文了解C#中的LINQ 2023-05-11
- C# 递归算法详解 2023-05-06
- centos7 部署netcore 2023-09-26
- C#如何优雅的结束线程 2023-04-15
- 图形学之Unity渲染管线流程分析 2023-05-12
- C#锁住文件的操作步骤 2023-01-27