本文介绍了SQL Union到NHibernate标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以将此SQL语句转换为NHibernate Criteria吗?



pre $ (选择b1.FieldA as Name,b1。 FieldA as Sale Full Name from Sale b1其中b1.FieldA like'%john%'或b1.FieldA like'%john%'order by b1.Id desc)
union
(选择b2.FieldC as Name, b2.FieldD作为FullName从销售b2其中b2.FieldC像'%john%'或b2.FieldD像'%john%'order by b2.Id desc)
union
(选择c.FieldE as名称,c.FieldF作为客户端c中的FullName,其中c.FieldE像'%john%'或c.FieldF像'%john%'order by c.Id desc)

我发现NHibernate不支持联合体。

解决方案

所以,我找到了两个解决方案。我分别执行每个查询,而不是对结果进行连接。它就像一个联盟,但不在数据库中执行,它在内存中执行。

  var b1 = Session.Query< Sale>()
.Where(x => x.FiledA.Contains(filter)|| x.FiledB.Contains(filter))
.OrderBy(x => x.Id)
.GroupBy(x => new {x.FiledA,x.FiledB})
.Select(x => new Foo {FullName = x.Key.FiledA,Name = x.Key.FiledB })
.Take(30)
.ToList();

var b2 = Session.Query< Sale>()
.Where(x => x.FiledC.Contains(filter)|| x.FiledD.Contains(filter))
.OrderBy(x => x.Id)
.GroupBy(x => new {x.FiledC,x.FiledD})
.Select(x => new Foo { FullName = x.Key.FiledC,Name = x.Key.FiledD})
.Take(30)
.ToList();


var c = Session.Query< Client>()
.Where(x => x.FiledE.Contains(filter)|| x.FiledF.Contains(过滤器))
.OrderBy(x => x.Id)
.GroupBy(x => new {x.FiledE,x.FiledF})
.Select(x => ; new Foo {FullName = x.Key.FiledE,Name = x.Key.FiledF})
.Take(30)
.ToList();

return b1.Concat(b2)
.Concat(c)
.ToList()
.GroupBy(x => new {x.Name,x .FullName})
.Select(x => x.First())
.Take(30);

OR

  var b1 = Session.CreateCriteria< Sale>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Distinct(Projections.Property(FiledA)),Name )
.Add(Projections.Property(FiledB),FullName))
.Add(Restrictions.Or(Restrictions.InsensitiveLike(FiledA,filter),
Restrictions .InsensitiveLike(FiledB,filter)))
.AddOrder(Order.Desc(Id))
.SetMaxResults(30)
.SetResultTransformer(Transformers.AliasToBean< Foo>( ))
.List< Foo>();

var b2 = Session.CreateCriteria< Sale>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Distinct(Projections.Property(FiledC)) ),Name)
.Add(Projections.Property(FiledD),FullName))
.Add(Restrictions.Or(Restrictions.InsensitiveLike(FiledC,filter),
Restrictions.InsensitiveLike(FiledD,filter)))
.AddOrder(Order.Desc(Id))
.SetMaxResults(30)
.SetResultTransformer(Transformers.AliasToBean< ; Foo>())
.List< Foo>();

var c = Session.CreateCriteria< Client>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Distinct(Projections.Property(FiledE)) ),Name)
.Add(Projections.Property(FieldF),FullName))
.Add(Restrictions.Or(Restrictions.InsensitiveLike(FiledE,filter),
Restrictions.InsensitiveLike(FieldF,filter)))
.AddOrder(Order.Desc(Id))
.SetMaxResults(30)
.SetResultTransformer(Transformers.AliasToBean< ; Foo>())
.List< Foo>(); (b)
.Concat(c)
.ToList()
.GroupBy(x => new {x.FullName,x .Name())
.Select(x => x.First())
.Take(30);


There's a way to convert this SQL statement to a NHibernate Criteria?

(select b1.FieldA as Name, b1.FieldA as FullName from Sale b1 where b1.FieldA like '%john%' or b1.FieldA like '%john%' order by b1.Id desc)
union
(select b2.FieldC as Name, b2.FieldD as FullName from Sale b2 where b2.FieldC like '%john%' or b2.FieldD like '%john%' order by b2.Id desc)
union
(select c.FieldE as Name, c.FieldF as FullName from Client c where c.FieldE like '%john%' or c.FieldF like '%john%' order by c.Id desc)

I've found that NHibernate doesn't support Unions.

解决方案

So, I've found two solutions. I perform each query separately than I concat the results. It's like a Union, but isn't performed in the DB, it's performed in memory.

var b1 = Session.Query<Sale>()
            .Where(x => x.FiledA.Contains(filter) || x.FiledB.Contains(filter))
            .OrderBy(x => x.Id)
            .GroupBy(x => new { x.FiledA, x.FiledB })
            .Select(x => new Foo { FullName = x.Key.FiledA, Name = x.Key.FiledB })
            .Take(30)
            .ToList();

var b2 = Session.Query<Sale>()
            .Where(x => x.FiledC.Contains(filter) || x.FiledD.Contains(filter))
            .OrderBy(x => x.Id)
            .GroupBy(x => new {x.FiledC, x.FiledD})
            .Select(x => new Foo {FullName = x.Key.FiledC, Name = x.Key.FiledD})
            .Take(30)
            .ToList();


var c = Session.Query<Client>()
            .Where(x => x.FiledE.Contains(filter) || x.FiledF.Contains(filter))
            .OrderBy(x => x.Id)
            .GroupBy(x => new { x.FiledE, x.FiledF })
            .Select(x => new Foo { FullName = x.Key.FiledE, Name = x.Key.FiledF })
            .Take(30)
            .ToList();

return b1.Concat(b2)
         .Concat(c)
         .ToList()
         .GroupBy(x => new { x.Name, x.FullName })
         .Select(x => x.First())
         .Take(30);

OR

var b1 = Session.CreateCriteria<Sale>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Distinct(Projections.Property("FiledA")), "Name")
        .Add(Projections.Property("FiledB"), "FullName"))
    .Add(Restrictions.Or(Restrictions.InsensitiveLike("FiledA", filter),
        Restrictions.InsensitiveLike("FiledB", filter)))
    .AddOrder(Order.Desc("Id"))
    .SetMaxResults(30)
    .SetResultTransformer(Transformers.AliasToBean<Foo>())
    .List<Foo>();

var b2 = Session.CreateCriteria<Sale>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Distinct(Projections.Property("FiledC")), "Name")
        .Add(Projections.Property("FiledD"), "FullName"))
    .Add(Restrictions.Or(Restrictions.InsensitiveLike("FiledC", filter),
        Restrictions.InsensitiveLike("FiledD", filter)))
    .AddOrder(Order.Desc("Id"))
    .SetMaxResults(30)
    .SetResultTransformer(Transformers.AliasToBean<Foo>())
    .List<Foo>();

var c = Session.CreateCriteria<Client>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Distinct(Projections.Property("FiledE")), "Name")
        .Add(Projections.Property("FieldF"), "FullName"))
    .Add(Restrictions.Or(Restrictions.InsensitiveLike("FiledE", filter),
        Restrictions.InsensitiveLike("FieldF", filter)))
    .AddOrder(Order.Desc("Id"))
    .SetMaxResults(30)
    .SetResultTransformer(Transformers.AliasToBean<Foo>())
    .List<Foo>();

return b1.Concat(b2)
         .Concat(c)
         .ToList()
         .GroupBy(x => new {x.FullName, x.Name})
         .Select(x => x.First())
         .Take(30);

这篇关于SQL Union到NHibernate标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:36
查看更多