3

Which one is better for getting the last row of my table with linq to nhibernate? Why?

    internal Bill GetLastBill()
    {
        var q = from b in session.Linq<Bill>()
                select b;

        return q.OrderByDescending(x => x.Id).First();
    }

OR

    internal Bill GetLastBill()
    {
        long maxId = session.Linq<Bill>().Max(i => i.Id);

        var q = from b in session.Linq<Bill>()
                where b.Id == maxId
                select b;

        return q.First();
    }
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
  • Is `session.Linq()` sorted in any fashion? – Guvante Jun 21 '11 at 18:38
  • 1
    You should a database profiler (or use NHProf) to see the generated query. They could both be using the same query. The only way to be sure is to profile it :) – tucaz Jun 21 '11 at 18:41

2 Answers2

3

Have you considered:

return session.Linq<Bill>().Last();

MSDN Reference

lsuarez
  • 4,952
  • 1
  • 29
  • 51
3

Of your two choices the first options is better because it does 1 query. Second option will execute 2 queries.

Todd
  • 1,461
  • 2
  • 13
  • 27