Nhibernate 计数不同(基于多列) [英] Nhibernate count distinct (based on multiple columns)

查看:24
本文介绍了Nhibernate 计数不同(基于多列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我一直在尝试这样做(根据两列计算不同的数):

Basically, i have been trying to do this (count distinct based on two columns):

select count(distinct(checksum(TableA.PropertyA, TableB.PropertyB))) 
from TableA 
left outer join TableB
on TableA.TableBId = TableB.Id 
where PropertyA like '%123%'

一直在谷歌上搜索如何做到这一点,但没有运气.试过这个,但从未真正奏效.根据两个表中的两个属性,这并不能明确计算:

Been googling on how to do this but with no luck. Tried this, but never actually worked. This does not count distinctly based on the two properties from two tables:

var queryOver = c.QueryOver<TableA>();
TableB tableBAlias = null;
TableA tableAAlias = null;
ProjectionList projections = Projections.ProjectionList();

queryOver.AndRestrictionOn(x => x.PropertyA).IsLike("%123%");
projections.Add(Projections.CountDistinct(() => tableAAlias.PropertyA));

queryOver.JoinAlias(x => x.TableB , () => tableBAlias, JoinType.LeftOuterJoin);
projections.Add(Projections.CountDistinct(() => tableBAlias.PropertyB));

queryOver.Select(projections);
queryOver.UnderlyingCriteria.SetProjection(projections);
return queryOver.TransformUsing(Transformers.DistinctRootEntity).RowCount();

推荐答案

好的,这需要几个步骤,请耐心等待.我在这里假设 SQL 服务器,但说明应该适用于任何支持 checksum1 的方言:

Okay this is going to take a few steps, so bear with me. I'm assuming SQL server here, but the instructions should work for any dialect that supports checksum1:

  1. 创建支持checksum功能的自定义方言:

public class MyCustomDialect : MsSql2008Dialect
{
    public MyCustomDialect()
    {
        RegisterFunction("checksum", new SQLFunctionTemplate(NHibernateUtil.Int32, "checksum(?1, ?2)"));
    }
}

  • 更新您的配置以使用自定义方言(您可以在配置 XML 文件中或使用代码执行此操作.请参阅 这个答案 了解更多信息).以下是我在现有配置代码中的做法:

  • Update your configuration to use the custom dialect (you can do this either in your configuration XML file or with code. See this answer for more information). Here's how I did it inside of my existing configuration code:

    configuration
        .Configure(@"hibernate.cfg.xml")
        .DataBaseIntegration(
            db => db.Dialect<MyCustomDialect>());
    

  • 创建一个调用 checksum 的自定义投影.这一步是可选的——如果你愿意,你可以直接调用 Projections.SqlFunction,但我认为将它重构为一个单独的函数更清晰:

  • Create a custom projection that calls checksum. This step is optional-- you can call Projections.SqlFunction directly if you'd like, but I think refactoring it into a separate function is cleaner:

    public static class MyProjections 
    {
        public static IProjection Checksum(params IProjection[] projections)
        {
            return Projections.SqlFunction("checksum", NHibernateUtil.Int32, projections);   
        }
    }
    

  • 编写您的 QueryOver 查询并调用自定义投影:

  • Write your QueryOver query and call the custom projection:

    int count = session.QueryOver<TableA>(() => tableAAlias)
        .Where(p => p.PropertyA.IsLike("%123%"))
        .Left.JoinQueryOver(p => p.TableB, () => tableBAlias)
        .Select(
            Projections.Count(
                Projections.Distinct(
                MyProjections.Checksum(
                    Projections.Property(() => tableAAlias.PropertyA),
                    Projections.Property(() => tableBAlias.PropertyB)))))
        .SingleOrDefault<int>();
    

    这应该会生成看起来像您所追求的 SQL:

    This should generate SQL that looks like what you're after:

    SELECT count(distinct checksum(this_.PropertyA, tableba1_.PropertyB)) as y0_
    FROM   [TableA] this_
        left outer join [TableB] tableba1_
        on this_.TableBId = tableba1_.Id
    WHERE  this_.PropertyA like '%123%' /* @p0 */
    


  • 1仍在尝试弄清楚是否有一种方法可以在不手动指定参数数量的情况下映射函数


    1Still trying to figure out if there's a way to map a function without manually specifying the number of arguments

    这篇关于Nhibernate 计数不同(基于多列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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