BlazingFrog
BlazingFrog

Reputation: 2435

'System.Linq.IQueryable' does not contain a definition for 'OrderByDescending'

Can someone explain why the statement compiles with OrderBy but not with OrderByDescending? Seems to me it should work: https://msdn.microsoft.com/en-us/library/vstudio/bb534316(v=vs.100).aspx

'System.Linq.IQueryable' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)

Target framework is .Net 4.5.1.
The type of entity is System.Data.Entity.DbSet

var comparisonQuery = new ComparisonQuery();
comparisonQuery.Query = entity.Where(whereStatement)

.OrderByDescending(GenOrderByFragment()). // no go

Select(GenBigRowResultObject(elements, idFieldName)).AsNoTracking();

Upvotes: 0

Views: 1765

Answers (2)

BlazingFrog
BlazingFrog

Reputation: 2435

@d-stanley your original comment sent me in the right direction. Turns out that OrderBy was implemented through a custom extension, not a framework's extension like I immediately assumed.
Thanks for the help.

Upvotes: 1

Rob Davis
Rob Davis

Reputation: 1319

As you elaborated in a comment above, GenOrderByFragment returns a string. However, OrderByDescending expects a Func<Entity, string>.

For example:

// this works
Func<Person, string> keySelectorGood = p => "Name";
var goodQuery = entityContext.People.Where(p => p.Name == "John").OrderByDescending(keySelectorGood);

// this DOES NOT WORK
Func<string> keySelectorBad = () => "Name";
var badQuery = entityContext.People.Where(p => p.Name == "John").OrderByDescending(keySelectorBad);

Upvotes: 0

Related Questions