I am trying to get the first item in the database that has the a given 'UserGuid', but the 'First' extension method is throwing the following exception: System.InvalidOperationException: Sequence contains no elements
我试图获得数据库中具有给定'UserGuid'的第一项,但'First'扩展方法抛出以下异常:System.InvalidOperationException:Sequence不包含任何元素
Here are some examples of what works and what does not:
以下是一些有效和无效的例子:
// Works
var FoundUser1 = MyEntities.Users.First();
// Works
var FoundUser3 = MyEntities.Users.ToList().First(r => r.UserGuid == SampleUserGuid);
// Throws System.InvalidOperationException: Sequence contains no elements.
var FoundUser2 = MyEntities.Users.First(r => r.UserGuid == SampleUserGuid);
Anyone have any ideas?
有人有主意吗?
EDIT
More weird code examples:
更奇怪的代码示例:
// UserList1 is empty
var Query1 = from x in MyEntities.Users
where x.UserGuid == criteria.Value
select x;
var UserList1 = Query1.ToList();
// UserList2 has 3 users, including one with a
// matching guid value.
var Query2 = from x in MyEntities.Users
select x;
var UserList2 = Query2.ToList();
var Query22 = from x in Query2
where x.UserGuid == criteria.Value
select x;
var UserList22 = Query22.ToList();
// Works
User FoundUser = null;
foreach (var ThisUser in MyEntities.Users)
{
if (ThisUser.UserGuid == criteria.Value)
FoundUser = ThisUser;
}
4 个解决方案
#1
What is the exception, is it an "Empty Set" exception or something similar?
什么是例外,它是一个“空集”异常还是类似的东西?
Try
FirstOrDefault()
Instead of
First()
and see what happens. First() will throw an exception if there are no records available. FirstOrDefault() will give you am empty User object if no records are available.
看看会发生什么。如果没有可用的记录,First()将抛出异常。如果没有可用的记录,FirstOrDefault()将为您提供空的User对象。
#2
In this piece of code:
在这段代码中:
// UserList1 is empty
var Query1 = from x in mgr.ObjectContext.Users
where x.UserGuid == criteria.Value
select x;
var UserList1 = Query1.ToList();
What is the value of criteria.Value, if you Debug.Print it?
criteria.Value的值是什么,如果你Debug.Print呢?
#3
Try comparing their string values, as in
尝试比较它们的字符串值,如
x.UserGuid.ToString() == criteria.Value.ToString()
There are other examples on the internet of people who have trouble comparing guids directly. Here is one:http://bytes.com/groups/net-vb/364009-comparing-guids
在互联网上还有其他例子,他们无法直接比较guid。这是一个:http://bytes.com/groups/net-vb/364009-comparing-guids
#4
It turns out the issue was with SQLite's handing of GUID types. If you set
事实证明问题在于SQLite处理GUID类型。如果你设置
BinaryGuids=True
then they are saved as 16 byte binaries and cannot be 'matched' against string based guids. You can also set that to be false and they match as strings, but you need to watch the case for the alpha chars.
然后它们被保存为16字节的二进制文件,并且不能与基于字符串的guid“匹配”。您也可以将其设置为false并将它们匹配为字符串,但您需要查看alpha字符的大小写。
#1
What is the exception, is it an "Empty Set" exception or something similar?
什么是例外,它是一个“空集”异常还是类似的东西?
Try
FirstOrDefault()
Instead of
First()
and see what happens. First() will throw an exception if there are no records available. FirstOrDefault() will give you am empty User object if no records are available.
看看会发生什么。如果没有可用的记录,First()将抛出异常。如果没有可用的记录,FirstOrDefault()将为您提供空的User对象。
#2
In this piece of code:
在这段代码中:
// UserList1 is empty
var Query1 = from x in mgr.ObjectContext.Users
where x.UserGuid == criteria.Value
select x;
var UserList1 = Query1.ToList();
What is the value of criteria.Value, if you Debug.Print it?
criteria.Value的值是什么,如果你Debug.Print呢?
#3
Try comparing their string values, as in
尝试比较它们的字符串值,如
x.UserGuid.ToString() == criteria.Value.ToString()
There are other examples on the internet of people who have trouble comparing guids directly. Here is one:http://bytes.com/groups/net-vb/364009-comparing-guids
在互联网上还有其他例子,他们无法直接比较guid。这是一个:http://bytes.com/groups/net-vb/364009-comparing-guids
#4
It turns out the issue was with SQLite's handing of GUID types. If you set
事实证明问题在于SQLite处理GUID类型。如果你设置
BinaryGuids=True
then they are saved as 16 byte binaries and cannot be 'matched' against string based guids. You can also set that to be false and they match as strings, but you need to watch the case for the alpha chars.
然后它们被保存为16字节的二进制文件,并且不能与基于字符串的guid“匹配”。您也可以将其设置为false并将它们匹配为字符串,但您需要查看alpha字符的大小写。