I was having an issue with a LINQ query today and after some debugging I was able to resolve the issue but it seems like a bug to me in the way LINQ works. Here is what I had:
var myitem = context .items .OrderByDescending(x => x.DateEdited) .FirstOrDefault(x => x.fkId == myFkId && x.DateEdited < someDate); In my database I have a table with some records and I want to retrieve the most recent record that is older than "someDate" and who has a particular foreign key in a column. The above query did not work however. It was returning the oldest record with the matching foreign key. I ended up having to rewrite my query like this to get it to work:
var myitem = context .items .Where(x => x.fkId == myFkId && x.DateEdited < someDate) .OrderByDescending(x => x.DateEdited) .FirstOrDefault(); In my debugging I found out that the "x.DateEdited < someDate" was re-ordering the IEnumerable so I ended up having to put my OrderByDescending clause after the date check.
Has anybody else run into this issue? Is it a bug or expected functionality?