Question

Hi I need help with making QueryOver in NHibernate from MYSQL. MYSQL code looks like this

select lietadlo.id,lietadlo.volne,spolocnost.name,typ.name,typ.miest from lietadlo
join spolocnost on spolocnost.id = lietadlo.spolocnostt_id
join typ on typ.id = lietadlo.typp_id
where spolocnost.pocetlietadiel > 2

And then how can i write it in Data Grid View ?

Edit: So I have done so far this and try it(works good)

ISessionFactory factory =
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
ISession session = null;
session = factory.OpenSession();

Lietadlo f = null;
Spolocnost t = null;
Typ r = null;

dgv.DataSource = session.QueryOver<Lietadlo>(() => f)
.JoinAlias(() => f.Spolocnostt_Id,() => t)
.JoinAlias(() => f.Typp_Id, ()=> r)
.Where(() => t.Pocetlietadiel > 2)
.And(() => r.Name == "Boeing-747")
.List<Lietadlo>()
.ToList<Lietadlo>();

But still in DataGridView I get only columns from Lietadlo and I want from Lietadlo only id(int),volne(int) and from Spolocnost name(string) and from Typ name(string) and miest(int).

Was it helpful?

Solution

I'd expect, that this is one way binding (just for reading). In this scenario you could profit from Projections. see more here 16.6. Projections

You can create some DTO Object for your grid and similar to documentation:

CatSummary summaryDto = null;
IList<CatSummary> catReport =
    session.QueryOver<Cat>()
        .SelectList(list => list
            .SelectGroup(c => c.Name).WithAlias(() => summaryDto.Name)
            .SelectAvg(c => c.Age).WithAlias(() => summaryDto.AverageAge))
        .TransformUsing(Transformers.AliasToBean<CatSummary>())
        .List<CatSummary>();

You should be able to do it like this (I cannot check it right now, but it should be clear)

LietadloDTO lietadloDTO = null;
dgv.DataSource = session
  .QueryOver<Lietadlo>(() => f)
  .JoinAlias(() => f.Spolocnostt_Id,() => t)
  .JoinAlias(() => f.Typp_Id, ()=> r)
  .Where(() => t.Pocetlietadiel > 2)
  .And(() => r.Name == "Boeing-747")
  .SelectList(list => list
    .Select(f => f.Id).WithAlias(() => lietadloDTO.Id)
    .Select(t => t.Name).WithAlias(() => lietadloDTO.Name)
    ...
    )
  .TransformUsing(Transformers.AliasToBean<LietadloDTO>())
  .List<LietadloDTO>()
  .ToList<LietadloDTO>();

So, in this case, you will force NHibernate to create Projection (only 1 SELECT clause) and return all needed data at once

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top