R – NHIbernate DataReader Equivalent

nhibernate

I'm somewhat new to NHibernate, so this question may be a no-brainer. Anyway, I'm replacing a project at work that uses ADO.NET that will now be using NHibernate(With LinqToNhibernate). This has worked fine so far, but I've run up against a function that needs to search through a large amount of data(about 200,000 records, test each record using a very specific algorithm(something too complicated for LINQ), and if it matches the criteria I want to add it to the results list. In ADO.NET I used a data reader so I wouldn't have to load all the results in memory. Is there an equivalent way to do this in NHibernate? Would the lazy loading be used for this? Thanks!

Best Answer

Ok from what I remember with the ISession.CreateQuery you can return a list or an enumerable. The List will load all of them into memory (I believe). The IEnumerable will issue a statement to the server to retrieve all of the Ids from the server. Every time you iterate through the ienumerable, it'll check the cache and if it's not in the cache, it'll issue a database call to retrieve the information.

Neither of them is what you want. You either use a ton of memory or hammer the DB.

I really sounds like you're trying to do some kind of ETL operation. Your best bet is to keep a couple of specialized pieces of code around to handle this situation.

If you insist on using NHibernate I would just page through the results in manageable chunks and use stateless sessions here:

http://davybrion.com/blog/2008/10/bulk-data-operations-with-nhibernates-stateless-sessions/