gt.guybrush gt.guybrush - 1 month ago 16
C# Question

Nhibernate queryover matching two IEnumerable

i have this domain objects:

public class Societa : EquatableObject<Societa>
{
public virtual int IdSocieta { get; set; }
public virtual string NomeSocieta { get; set; }
}

public class Attivita {
public virtual int IdAttivita { get; set; }
public virtual IEnumerable<ProcessoEsaminato> Processi
}

public class ProcessoEsaminato {
public virtual ProcessoSocieta ProcessoCoperto { get; set; }
public virtual int Anno { get; set; }
}

public class ProcessoSocieta {
public override int Id { get; set; }
public virtual Societa SocietaDiretta { get; set; }
public virtual Societa SocietaService { get; set; }
}

public class Processo {
public virtual int Id { get; set; }
public virtual string NomeProcesso { get; set; }
public virtual IEnumerable<ProcessoSocieta> SocietaAttivate
}


i nedd to extract from db with QueryOver or LinqToNHibernate every
Process
of an
Attivita
with
NomeProcesso
,
SocietaDiretta.NomeSocieta
and
SocietaService.NomeSocieta


So i think:
i have to start from
Processo
and get those that in their
SocietaAttivate
has one that is in
Processi
collection of
Attivita
, looking at
ProcessoCoperto
property of every element of this collection

i try this:

public IEnumerable<object> ProcessiPerAttivita (Attivita att) {
ProcessoSocieta ps = null;
var elencoPS = att.Processi.Select(p => p.ProcessoCoperto).ToList<ProcessoSocieta>();

return _session.QueryOver<Processo>()
.JoinAlias(processo => processo.SocietaAttivate, () => ps)
.Where(x => x.SocietaAttivate.IsIn(elencoPS))
.List();
}


but
Where(x => x.SocietaAttivate.IsIn(elencoPS))
is not what i nedd, since it wants only a list of id. so first question is how can i do this?

Second question is how can i select only fields i need from different object, coming from different level of aggregation?

EDIT:

now i try

_session.QueryOver<Processo>()
.JoinAlias(processo => processo.SocietaAttivate, () => ps)
.Where(x => x.SocietaAttivate.Any(p => elencoPS.Contains(p)) != null)
.List();


but i get
variable 'x' of type 'ProcessoSocieta' referenced from scope '', but it is not defined

Answer

Try this:

    public IEnumerable<Processo> ProcessiPerAttivita (Attivita att) {     
    ProcessoSocieta ps = null;
    var elencoPS = att.Processi.Select(p => p.ProcessoCoperto).ToList<ProcessoSocieta>();

    return _session.QueryOver<Processo>()           
        .JoinAlias(processo => processo.SocietaAttivate, () => ps)
        .WhereRestrictionOn(processo  => ps.Id).IsIn(elencoPS.Select(el => el.Id).ToList())
        .List<Processo>();            
}

You must use the 'ps' alias!

Edit: you can use

.List<Processo>(); and return an IEnumerable<Processo>
Comments