简体   繁体   中英

Getting “Sequence contains no elements” exception

I am new to .net framework, nhibernate and iqueryable.

I have a um_user_login_count table which has 4 columns. They are id, login_ip, login_count and login_user_id.

Here is my UserLoginCount.cs

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.UserManagement" namespace="Project.UserManagement.Domain" schema="UserManagement">
  <class name="UserLoginCount" table="um_user_login_count">
    <id name="Id" column="id" generator="identity" />
    <property name="Ip" column="login_ip" length="255" />
    <property name="Count" column="login_count" length="255" />
    <property name="UserId" column="login_user_id" />
  </class>
</hibernate-mapping>

Here is my LoginCount.cs file

namespace Project.UserManagement.Domain
{
    using System;
    using Iesi.Collections.Generic;
    using Project.ORM;

    public class UserLoginCount : UserManagementEntity<UserLoginCount>
    {
        public UserLoginCount() { }

        public virtual int Id { get; set; }
        public virtual string Ip { get; set; }
        public virtual string Count { get; set; }
        public virtual string UserId { get; set; }
    }
}

Here is what I have declared in the UMDataService code

public static IQueryable<UserLoginCount> UserLoginCount
        {
            get
            {
                return DataServiceInstance.CurrentSession.Query<UserLoginCount>();
            }
        }

Here si the code from where I am called the service -

public string GetLoginInfo(string id)
        {
            UserLoginCount cnt = UMDataServices.UserLoginCount.Single();
            return cnt.Ip;
        }

Yet I am getting an error saying "Sequence contains no elements". I am absolutely new at this. I am not sure I have even asked the question properly.

UMDataServices.UserLoginCount.Single() expects that there must be exactly 1 entry. In your case, there is no entry and the exception is thrown as expected.

There are other options: SingleOrDefault (), First (), FirstOrDefault ,...

You could choose one depending on your requirement.

This line UserLoginCount cnt = UMDataServices.UserLoginCount.Single();

is probably the culprit. You could change it to SingleOrDefault and check for null.

UserLoginCount cnt = UMDataServices.UserLoginCount.SingleOrDefault();
if(cnt != null) { /*do stuff */}

Single throws an exception under following circumstances:

ArgumentNullException

  • source or predicate is null.

InvalidOperationException

  • No element satisfies the condition in predicate
  • More than one element satisfies the condition in predicate.
  • The source sequence is empty.

You can prevent it with SingleOrDefault .

 UserLoginCount cnt = UMDataServices.UserLoginCount.SingleOrDefault();
 string ip = null;
 if(cnt != null)
      ip = cnt.Ip;
 return ip;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM