Apply between in an hql in c#

Asked

Viewed 351 times

2

I am trying to apply a between with two fields of a part of my form, are dates and vehicle ids, I do not have much knowledge of how it models this by HQL in nhibernate with c#, I wonder how it would be to apply this in hql.

Usually 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 later use in controller I apply as follows:

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

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

  • what is the name of the Date column?

  • recommend reading https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/queryhql.html

  • @Leandroangelo The column name is Dtabastecido

2 answers

3


Follow 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>();
}
  • On the setParameter part, will he take my Viewbag that contains my values? That’s what setParameter does then?

  • It will take the values you pass as parameter in the Query method

  • Since your question was about hql, that would be the approach.

  • Is that this line setParameter, I saw in some examples using login and user password, but did not know how it was used, now I think I understand how it works

  • I think sometimes you are confusing what is bank, entity and when you are interacting with each.

  • Actually I’m starting recently with c#, I’m a little confused with it, and I still have the framework that’s making it difficult for me to understand the interaction between the bank and the entities

Show 2 more comments

1

You can use the expression "Where" and use the Isbetween attributes to pick up a result within an estimated date.

Where(r => r.data.IsBetween(dataInicio).And(dataFim)).List();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.