Domanda

Here I get a list of of agencies a rep has access to.

var agencies = _session.QueryOver<AgencyRep>()
    .JoinQueryOver<AgencyRepsSecondaryAgency>(r => r.AgencyRepsSecondaryAgencies)
    .Where(a => a.AgencyRepId == user.Id)
    .List();

After I get my list of agencies I want to do a SQL Search where the value is IN the agencies.id column.

var data = _session.QueryOver<Staging.Case>()
                .Where(x => x.AgencyId ???? agencies)
                .List();

Not sure how to implement something like this with fluent nhibernate. Any help would be greatly appreciated.

È stato utile?

Soluzione

Try this:

var data = _session.QueryOver<Staging.Case>()
            .WhereRestrictionOn(x => x.AgencyId).IsIn(agencies.Select(a => a.Id).ToArray())
            .List();

EDIT: If you don't need the agencies afterwards - you can optimize a bit by going:

var agencyIds = _session.QueryOver<AgencyRep>()
    .JoinQueryOver<AgencyRepsSecondaryAgency>(r => r.AgencyRepsSecondaryAgencies)
    .Where(a => a.AgencyRepId == user.Id)
    .Select(a => a.Id)
    .List<long>(); //if your .Id is of type long - replace with the correct type

var data = _session.QueryOver<Staging.Case>()
            .WhereRestrictionOn(x => x.AgencyId).IsIn(agencyIds.ToArray())
            .List();

That saves a bit of data sent over the connection

Altri suggerimenti

This should work, is untested but you can try:

var subquery = QueryOver.Of<AgencyRep>()
    .JoinQueryOver<AgencyRepsSecondaryAgency>(r => r.AgencyRepsSecondaryAgencies)
    .Where(a => a.AgencyRepId == user.Id)

var data = _session.QueryOver<Staging.Case>()
    .WithSubquery.WhereProperty(x => x.AgencyId).In(subquery)
    .List();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top