简体   繁体   中英

Simplify multiple nhibernate queryover

I'm doing a query over two different tables. In the first query, i get some Ids that I then have to check in another table. Then I do the first query again with the result of the second query.

This can't be the best way to do this.

But I haven't found a good way to solve it. So some help would be appreciated.

IntOrderInvoiceCostOut y = null;
var list = session.QueryOver<IntOrderInvoiceCostOut>(() => y)
                .Where(x => x.IntegrationHandleDate == null)
                .Select(Projections.Distinct(Projections.Property(() => y.Externalid)))
                .List<string>();
var nonPreliminaryOrders = session.QueryOver<RefImplOrderEntity>()
                .WhereRestrictionOn(x => x.ExternalId).IsIn(list.ToList())
                .Where(x => x.StatusTypeId != 95)
                .Select(x => x.ExternalId)
                .List<string>();
var finalList = session.QueryOver<IntOrderInvoiceCostOut>()
                .WhereRestrictionOn(x => x.Externalid).IsIn(nonPreliminaryOrders.ToList())
                .Where(x => x.IntegrationHandleDate == null)
                .OrderBy(x => x.IntegrationCreateDate)
                .Asc
                .List();

The code works...but i't really ugly.

you could use detacheCriteria for this. I have omitted couple of conditions and you might have to twick a bit as per your requirement.

for example

                IntOrderInvoiceCostOut y = null;
var list = QueryOver.Of<IntOrderInvoiceCostOut>(() => y)
                .Where(x => x.IntegrationHandleDate == null)
                .Select(Projections.Distinct(Projections.Property(() => y.Externalid)))
                .DetachedCriteria;


var nonPreliminaryOrders = QueryOver.Of<RefImplOrderEntity>()
                            .Where(Subqueries.PropertyIn(nameof(RefImplOrderEntity.ExternalId), list));
                              .Select(x => x.ExternalId)
                            .DetachedCriteria



var finalList = session.QueryOver<IntOrderInvoiceCostOut>()
                    .Where(Subqueries.PropertyIn(nameof(IntOrderInvoiceCostOut.ExternalId), nonPreliminaryOrders));
                    .List();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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