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;
}