繁体   English   中英

NHibernate QueryOver顺序通过算术运算

[英]NHibernate QueryOver order by with arithmetic operations

我已经建立了一个HQL以使用CreateQuery查询某些实体,并且正在尝试使用QueryOver将其转换为lambda表达式。

该查询在LongitudeLatitude范围内查找Negocio ,并根据用户和Negocio之间的距离对它们进行Negocio 我想将其转换为lambda表达式,因为它更易于维护,但我的工作依部分顺序而定。

这些是我拥有的实体。 我已经忽略了在这种情况下不相关的内容。

public class Negocio
{
   public Endereco Endereco {get; set;} 
}

public class Endereco
{
    public GeoCoordenada GeoCoordenada {get; set;}
}
public class GeoCoordenada
{
   public double Latitude {get; set;}
   public double Longitude {get; set;}
}

这是执行魔术的查询:

query = _session.CreateQuery(@"select distinct(n),
 SQRT(SQUARE(e.GeoCoordenada.Latitude - :lat) + SQUARE(e.GeoCoordenada.Longitude - :lon)) 
             from Negocio n inner join n.Endereco as e where 
             (e.GeoCoordenada.Latitude between (:minLat) and (:maxLat)) and 
             (e.GeoCoordenada.Longitude between (:minLon) and (:maxLon))
             order by SQRT(SQUARE(e.GeoCoordenada.Latitude - :lat) + 
             SQUARE(e.GeoCoordenada.Longitude - :lon))");`

        query.SetParameter("minLat", -90);  // random values just  
        query.SetParameter("maxLat", 90);   // for testing
        query.SetParameter("minLon", -180); 
        query.SetParameter("maxLon", 180);
        query.SetParameter("lat", 25);
        query.SetParameter("lon", 25);

这是我到目前为止的事情:

 var query = _session.QueryOver<Negocio>()
                .JoinQueryOver(x => x.Endereco)
                .WhereRestrictionOn(x => x.GeoCoordenada.Latitude).IsBetween(-90).And(90)
                .AndRestrictionOn(x => x.GeoCoordenada.Longitude).IsBetween(-180).And(180)

重要提示:我无法在调用List()之后对它们进行排序,因为分页函数将使用skip()take()在查询的末尾添加,因此,一次查询所有结果毫无意义。

关于如何实现该命令的任何想法?

您可以使用ArithmeticOperatorProjection ,它是为NHibernate编写的一个小扩展: http : //savale.blogspot.ru/2011/04/nhibernate-and-missing.html

您的查询如下所示:

_session.QueryOver<Negocio>()
    .JoinQueryOver(x => x.Endereco)
    .JoinQueryOver(x => x.GeoCoordenada)
    .WhereRestrictionOn(x => x.Latitude).IsBetween(-90).And(90)
    .AndRestrictionOn(x => x.Longitude).IsBetween(-180).And(180)
    .OrderBy(
        new ArithmeticOperatorProjection(
            "+",
            NHibernateUtil.Decimal,
            new ArithmeticOperatorProjection(
                "*",
                NHibernateUtil.Decimal,
                new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Property<GeoCoordenada>(x => x.Longitude), Projections.Constant(25m)),
                new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Property<GeoCoordenada>(x => x.Longitude), Projections.Constant(25m))
            ),
            new ArithmeticOperatorProjection(
                "*",
                NHibernateUtil.Decimal,
                new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Property<GeoCoordenada>(x => x.Latitude), Projections.Constant(25m)),
                new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Property<GeoCoordenada>(x => x.Latitude), Projections.Constant(25m))
            )
    )
    .List();

可以通过添加自定义SQRTSQUARE投影来进一步改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM