views:

239

answers:

1

I want to retrieve list of roles for a logged in user.

Following is a code segment that reads user roles from the database.

ISession session = NHibernateHelper.GetCurrentSession();
            var data = from s in session.Linq<ApplicationUserRole>()
                    where s.AppUser.ID = 1
                    select s.Role.Name;

             List<Role> list = data.ToList();

AppUser: User entity Role: Role entity. As there are no data in the database for user id 1, it doesn't return anything.

Return type data is NHibernate.Linq.Query and it is not null.

It throws following error when I attempt to convert it to ToList();

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

How do I handle empty result sets?

+1  A: 

This should work...

List<Role> list = data.Any() ? data.ToList() : new List<Role>();
Hainesy
No, it just blows up on Any() with the exact same error message. From what I've seen, I think this is NHibernate Linq's way of telling you "I can't deal with your expression".
Charlie Flowers