First of all, I'm new to NHibernate. I have searched about the QueryOver thing in many pages, but the most profitable page to me has been http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html. Thanks to it, I have arrived to a "semi-solution" I think, but I need this solution to be better.
I have these classes:
public class Variable
{
public virtual int Id {get; set; }
public virtual string Nombre { get; set; }
public virtual string Descripcion { get; set; }
public virtual IList<ValorVariable> Valores { get; set; }
public virtual bool Temporal { get; set; }
public virtual bool Eliminado{ get; set; }
}
public class ValorVariable
{
public virtual int Id {get; set; }
public virtual int IdVariable { get; set; }
public virtual Variable Variable { get; set; }
public virtual DateTime FechaValor { get; set; }
public virtual decimal Valor { get; set; }
}
public class VariableLigera
{
public virtual int Id {get; set; }
public string Nombre { get; set; }
public string Descripcion { get; set; }
public bool Temporal { get; set; }
public Decimal Valor { get; set; }
}
Variable is the "important" class, that, among other things, have a IList of "ValorVariable"s, that have a value("Valor") and a value date("FechaValor"). "VariableLigera" is like a "light" class of variable, that have some properties of Variable and one value of the ValorVariable List. I hope that is clear.
To populate VariableLigera, I'm trying to do a QueryOver. I would like to do something like this:
Variable variableAlias = null;
VariableLigera variableLigeraAlias = null;
var result = _session
.QueryOver(() => variableAlias ).Where(x => x.Eliminado == false)
.Select(
Projections.Property(() => variableAlias .Id).WithAlias(() => variableLigeraAlias .Id),
Projections.Property(() => variableAlias .Nombre).WithAlias(() => variableLigeraAlias .Nombre),
Projections.Property(() => variableAlias .Descripcion).WithAlias(() => variableLigeraAlias .Descripcion),
Projections.Property(() => variableAlias .Temporal).WithAlias(() => variableLigeraAlias .Temporal),
Projections.Property(() => variableAlias .Valores.FirstOrDefault().Valor).WithAlias(() => variableLigeraAlias .Valor)
)
.TransformUsing(Transformers.AliasToBean<VariableLigera>())
.List<VariableLigera>();
And here is the problem. I would like to put into the property "Valor" of the variableLigeraAlias the FirstOrDefault value of the Valores list of variableLigera. But it throws an exception, "function FirstOrDefault not recognized".
So I tried another thing, create an alias to ValorVariable and join it to the query. Like this:
Variable variableAlias = null;
VariableLigera variableLigeraAlias = null;
ValorVariable valorVariableAlias = null;
var result = _session
.QueryOver(() => variableAlias).Where(x => x.Eliminado == false)
.JoinAlias(() => variableAlias.Valores, () => valorVariableAlias)
.Select(
Projections.Property(() => variableAlias.Id).WithAlias(() => variableLigeraAlias.Id),
Projections.Property(() => variableAlias.Nombre).WithAlias(() => variableLigeraAlias.Nombre),
Projections.Property(() => variableAlias.Descripcion).WithAlias(() => variableLigeraAlias.Descripcion),
Projections.Property(() => variableAlias.Temporal).WithAlias(() => variableLigeraAlias.Temporal),
Projections.Property(() => valorVariableAlias.Valor).WithAlias(() => variableLigeraAlias.Valor)
)
.TransformUsing(Transformers.AliasToBean<VariableLigera>())
.List<VariableLigera>();
With this query, I get results, but I get each variable many times(one duplicate per value in the ValoresVariables list). For example, if one variable have 3 values, the query will return 3 "VariablesLigera"s with the 3 values, but I only want one with the first value of it's list.
In short, and to make it clear, I would like to have one result per variable with the firstordefault value in the values("Valor") list. Is it possible using QueryOver? Thanks a lot.
PS: Since english it's not my native language, maybe some things are not much understandable. Feel free to ask if any doubt. Thanks again.