Zsombor V
Zsombor V

Reputation: 19

CS1061: "IEnumerable<DB-entity-model>" does not contain a definition for 'OrderByDescending()'

Db model: root/ Entities/ ThreadEntity.cs .

root/ Data/ IThreadEntity.cs

IEnumerable<ThreadEntity> GetLatestThreads(int amount); // to get/ load at once.

root/ Controllers/ ThreadController.cs , HomeController.cs , etc.

In the latter:

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;
    private readonly IThreadEntity _threadEntityService;

    public HomeController(ILogger<HomeController> logger, IThreadEntity threadEntityService)
    {
        _logger = logger;
        _threadEntityService = threadEntityService;
    }

    private HomeIndexModel BuildHomeIndexModel()
    {
        var latestThreads = _threadEntityService.GetLatestThreads(amount: 10);
        //...
    }
//...
}

root/ Services/ ThreadService.cs

public class ThreadService : IThreadEntity
{
//...
    public IEnumerable<ThreadEntity> GetLatestThreads(int amount)
    {
        return GetAll().OrderByDescending<ThreadEntity> (thread => thread.CreatedAt).Take(amount);
    }
//...
}

I get an error here

CS1061: IEnumerable<ThreadEntity> does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type IEnumerable<ThreadEntity> could be found.

I already checked the error code on msdn. What am I missing? Here is the rest of my code if it helps.

Upvotes: 0

Views: 260

Answers (1)

canton7
canton7

Reputation: 42320

Enumerable.OrderByDescending takes 2 type parameters: it's OrderByDescending<TSource, TKey>.

When you call OrderByDescending<ThreadEntity>, you're only passing 1 type parameter. The method OrderByDescending<T> doesn't exist, so you're getting an error.

If you want to specify type parameters, you need to specify both: OrderByDescending<ThreadEntity, T> where T is whatever type thread.Created is.

However, the C# compiler is good at inferring type parameters, so you should just be able to write:

return GetAll().OrderByDescending(thread => thread.Created).Take(a);

Upvotes: 1

Related Questions