Apply between a hql in c #

2
I'm trying to apply an between with two fields of a part of my form, are dates and ids of vehicles, I'm not very aware of how it models this by HQL in nhibernate with C #, I wanted to know what it would be like to apply this to hql.

Normally my queries using HQL are like this:

public IList<Abastecimento> ConsultaData(DateTime DtAbastecido)
    {
        string hql = "SELECT a FROM Abastecimento a";
        IQuery query = session.CreateQuery(hql);
        return query.List<Abastecimento>();
    }

Using query like this I then use the controller as follows:

var Rota = ckm.ConsultaProduto(viewModel.NumCarroId);

I wanted to know how I would put a between bringing dates, an example would be to bring me a record from 01/11/2017 until 01/12/2017, I wanted to know how the HQL query would be applying the between with two textbox that I will use in the form.

    
asked by anonymous 19.12.2017 / 16:24

2 answers

3

Here's an example

public IList<Abastecimento> ConsultaPeriodo(DateTime dataInicio, DateTime dataFim)
{
    string hql = "SELECT a FROM Abastecimento a WHERE a.DtAbastecimento BETWEEN :dataInicial AND :dataFinal";
    IQuery query = session.CreateQuery(hql)
        .SetParameter("dataInicial", dataInicio)
        .SetParameter("dataFinal", dataFinal);
    return query.List<Abastecimento>();
}
    
19.12.2017 / 18:33
1

You can use the expression "Where" and use the IsBetween attributes to get a result within an estimated date.

Where(r => r.data.IsBetween(dataInicio).And(dataFim)).List();
    
21.12.2017 / 13:07