开发者

How to handle NHibernate LINQ empty result set?

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 emp开发者_运维百科ty result sets?


This should work...

List<Role> list = data.Any() ? data.ToList() : new List<Role>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜