views:

282

answers:

4

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

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;
}
+3  A: 

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.

Robert Harvey
The 'set' is empty, but the question is 'why' is the set empty when there is valid data there (as shown by all the 'working' examples).
JasonRShaver
A: 

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?

Robert Harvey
The criteria one is: {e7bf9773-8231-44af-8d53-e624f0433943}The 3 in my sample data list (that show up in UserList2) are:{7ab8196e-f0bd-4db3-b6b4-3513c18b7c7f}{1dd40287-35a4-42cd-a380-c458c8ca7e62}{e7bf9773-8231-44af-8d53-e624f0433943}
JasonRShaver
That formatted better when I typed it. =)
JasonRShaver
What are the types of UserGuid and Value? Are they both string?
Robert Harvey
Both types are 'Guid' and I tried it with "where x.UserGuid.Equals(criteria.Value)" without any luck as well.
JasonRShaver
A: 

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

Robert Harvey
A: 

It turns out the issue was with SQLite's handing of GUID types. If you set

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.

JasonRShaver
Comparing with ToString() should always work, regardless of the BinaryGuids setting.
Robert Harvey