Nhibernate queryover matching two IEnumerable

微笑、不失礼 提交于 2019-12-12 02:19:12

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/40172663/nhibernate-queryover-matching-two-ienumerable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!