gpt4 book ai didi

c# - NHibernate/MySQL 字符串连接

转载 作者:可可西里 更新时间:2023-11-01 07:04:47 24 4
gpt4 key购买 nike

我有一个类似这样的 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

这爆炸了,说不支持 Concat,但是当我把它改成

 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 字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com