views:

52

answers:

1

Hi,Guys,i have a question about Criteria method,one-to-many relation to the database,'one' is "account",'many' is "sites",when i use CreateCriteria(),something appears let me in trouble.

Like this:"SessionFactory.OpenSession().CreateCriteria(typeof(Account)).List().Count();"

before it's run,i think the sql should be "Select count(*) from table",but the sql is "Select id,siteurl...from table",so what's wrong about this ? how can l solve it?

And First() method should be "Select top1 ...from table",but "Select ...from table "

I'm Nhiberate rookie ,Please Help me. thx!!

+3  A: 

This happens because the Count method you are calling at the end executes after the query has run and outside of the database. You are only counting the elements in the list in memory. To achieve what you are looking for you could use a projection:

var count = session
    .CreateCriteria<Account>()
    .SetProjection(
        Projections.Count(Projections.Id())
    )
    .UniqueResult<long>();
Darin Dimitrov
thx a lot!!!!!!
Jo Hao