NHibernate的/ MySQL的字符串连接 [英] NHibernate / MySQL string concatenation

查看:434
本文介绍了NHibernate的/ MySQL的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NHibernate的LINQ查询,看起来像这样:

I have a nhibernate linq query that looks like this:

 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

这吹了,说的毗连不支持,但是当我将其更改为

This blows up, saying Concat isn't support, but when I change it to

 from b in session.Query<Bookmark>()
where b.Uri.Equals(uri) ||
      b.Uri.Equals("www." + uri) ||
      ("www." + b.Uri).Equals(uri)
select b

运行良好,但查询看起来是这样的:

It runs fine, but the query looks like this:

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中为NHibernate的字符串为MySQL

Where the 'www.'+bookmark0_.Uri is "added" instead of concat('www.',bookmark0_.Uri). Is there a way to concatenate strings in Linq for NHibernate for MySQL?

推荐答案

以下是解决这一问题的HqlGenerator:

The following is an HqlGenerator that solves this problem:

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

Add this to your HQLGeneratorsRegistry and you will be good to go with calls to string.Concat in you LINQ statements.

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

这篇关于NHibernate的/ MySQL的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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