Get List by Id’s in Nhibernate

Hello Folks ,

As we know that we do not need to write separate method to get an entity Object by Id in Nhibernate . It’s already provided by Nhibernate . Many times we have  more than one Id’s and we want to get data object for them …

Well here is a simple probably general method (if you add it to your base repository) for all of your objects (which has “Id” column)

Public IEnumerable Get(IEnumerables ids)

{

var myCriteria = Session.CreateCriteria().Add(Restrictiona.In(“Id”, ids.ToArray()));

// here you  can put your logic ..

return myCriteria.List();

}

 

About these ads

2 thoughts on “Get List by Id’s in Nhibernate

  1. It is a bad method to do so, because the method generates IN statement and if there are more than 2100 items in Id list you will get an exception in MS SQL Server

    Reply
    • I agree with it .. but i am not able to think of any scenario when you need to pass 2100 ids as in a query… and I think if you do than there certainly something wrong with the implementation approach..

      Thanks for feedback on it anyways
      Have a great day..

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s