0

I'm trying to find a record that I know exist in the database. When searching for it using this query nothing is found.

List<OrganizationALE> ales =
                _ctx.OrganizationALEs.Where(c => c.OrganizationId.Equals(organizationId) && c.LastModified.Equals(modified) && c.StartDate.Equals(start)).ToList();

But when I search using this query it is clearly there in the result using watch. enter image description here

And if I try pull it out on the very next line using

var found = ales.First(a => a.LastModified == modified);

I get an exception say Sequence contains no matching element

Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188

1 Answers1

2

I bet it's because modified and/or start contains milliseconds that are not stored with the same precision in the database (datetime in SQL Server?) so that the comparison fails in the database.

Possible solutions are here (the second and third point apply to your situation). Basically supply less precise DateTime values (cut off the millisecond) or use datetime2(7) in SQL Server or avoid using equality comparisons with DateTime values and use >= and <= instead.

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420