Skip to content

OrderBy and OrderByDescending, with FirstOrDefault on a column with duplicate values #281

@joshakaw

Description

@joshakaw

I have an Access database with duplicate fields in the HappenedOn column that I wish to order by in my LINQ queries. When it is sorted descending (as shown in the screenshot below), there are two duplicate dates. When sorted ascending, there are also two duplicate dates.

Image

When I run either OrderBy or OrderByDescending,

  var currentMeeting = _context.MeetingT
       .OrderBy(m => m.HappenedOn)
       .Select(m => m.MeetingId).FirstOrDefault();
  var currentMeeting = _context.MeetingT
       .OrderByDescending(m => m.HappenedOn)
       .Select(m => m.MeetingId).FirstOrDefault();

I get an exception that is unexpected for the method FirstOrDefault, "Sequence contains more than one element". This does not happen when I order by a field that has no duplicates, like the MeetingId column. I may be mistaken, but I believe that an arbitrary record should be returned if there are two records that have the same value in the sort field.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions