栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从SQL Union到NHibernate标准

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从SQL Union到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(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);

要么

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号