[英]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
InvalidOperationException 出现InvalidOperationException
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.