简体   繁体   English

获取“序列不包含任何元素”异常

[英]Getting “Sequence contains no elements” exception

I am new to .net framework, nhibernate and iqueryable. 我是.net框架的新手,它是nhibernate和iqueryable。

I have a um_user_login_count table which has 4 columns. 我有一个um_user_login_count表,其中有4列。 They are id, login_ip, login_count and login_user_id. 它们是id,login_ip,login_count和login_user_id。

Here is my UserLoginCount.cs 这是我的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 这是我的LoginCount.cs文件

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 这是我在UMDataService代码中声明的内容

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. UMDataServices.UserLoginCount.Single()期望必须完全有 1个条目。 In your case, there is no entry and the exception is thrown as expected. 在您的情况下,没有条目,并且按预期引发异常。

There are other options: SingleOrDefault (), First (), FirstOrDefault ,... 还有其他选项: SingleOrDefault (), First (), FirstOrDefault ,...

You could choose one depending on your requirement. 您可以根据需要选择一个。

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

is probably the culprit. 可能是罪魁祸首。 You could change it to SingleOrDefault and check for null. 您可以将其更改为SingleOrDefault并检查是否为空。

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

Single throws an exception under following circumstances: 在以下情况下, Single会引发异常:

ArgumentNullException ArgumentNullException

  • source or predicate is null. source或谓词为null。

InvalidOperationException 出现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 . 您可以使用SingleOrDefault阻止它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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