I need to retrieve all the records in a table with nHibernate. If I had the key for all the records in the table I could loop and use nHibernate's Get
method (this seems inefficient though) but I don't have the keys. I could also use FindAll
but this requires criteria or a stored procedure.
How can I get all the records from the table?
SQL tables are mapped to classes so in order to retrieve all records from a table you write a query (HQL or Criteria) that fetches all the objects for a given typed mapped to this table:
var products = session.CreateCriteria<Product>().List<Product>();
or using HQL:
var products = session.CreateQuery("from " + typeof(Product)).List<Product>();
or LINQ:
var products = session.Linq<Product>().ToList() // 2.x contrib provider
var products = session.Query<Product>().ToList() // 3.x integrated provider
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With