0

Let's say i have following tables in database:

image

I want to get a list of all the children with a specific dadId and also i want to re-use this method for other criterias.

Is it something like this that i would use ?

 public IList<T> FindBy(Expression<Func<T, bool>> expression)
    {
        return Session.CreateCriteria<T>()
           //add restriction
            .List<T>();
    }
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Henrik
  • 1,797
  • 4
  • 22
  • 46

2 Answers2

1

No.

list<T> may return list<Object[]> depending on the Criteria you have.

You can write a wrapper but this is a lot of Overhead.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Okey, do you have a better solution then? – Henrik Feb 25 '14 at 15:18
  • Im new to NHibernate and im not sure what you mean with wrapper. Can you be a little more specific please? Thank you. – Henrik Feb 25 '14 at 15:48
  • A wrapper is a bigger kind of the Adapter-Patter from Gang-of-Four. See http://stackoverflow.com/questions/889160/what-is-a-wrapper-class – Grim Feb 26 '14 at 07:05
  • I am a little bit confused... you are using generics like a pro, but dont know about wrappers?! :D .... maybe i am just old! – Grim Feb 26 '14 at 07:07
  • Haha,I know what a wrapper is, but im not sure how to use it in this case. Will it be like this: return Session.CreateCriteria() and .List because it complains on having T in CreateCriteria ? :) – Henrik Feb 26 '14 at 07:37
0
public IList<T> GetAll<T>()
     where T : class
{
    return _session.CreateCriteria<T>().List<T>();
}

Works fine for me! Vote! Similar answer here - Why can't I use generics with CreateCriteria in NHibernate?

Any reason why i didnt get a vote on this? this code works for me and it appears to be exactly that the user is looking for. Did i miss something?

Community
  • 1
  • 1