Avatar ·

Quelle est la différence entre IEnumerable<T> et IQueryable<T> en C# et quand utiliser quoi ?

Bonjour, collègues ! Je suis débutant en C# et je travaille avec Entity Framework. Je confonds toujours quand retourner IEnumerable et quand retourner IQueryable. Voici une méthode dans mon repository : ```csharp public IEnumerable GetProductsByCategory(int categoryId) { return _context.Products.Where(p => p.CategoryId == categoryId).ToList(); } ``` Mais un autre développeur a dit qu'il vaut mieux utiliser IQueryable. J'ai réécrit : ```csharp public IQueryable GetProductsByCategory(int categoryId) { return _context.Products.Where(p => p.CategoryId == categoryId); } ``` Maintenant, j'ai un service qui appelle cette méthode et filtre ensuite : ```csharp var cheapProducts = repo.GetProductsByCategory(1).Where(p => p.Price < 100).ToList(); ``` Dans le premier cas (avec IEnumerable), tout fonctionne mais lentement. Dans le second (avec IQueryable), c'est plus rapide, mais je ne comprends pas pourquoi. Expliquez-moi, s'il vous plaît, quelle est la différence fondamentale ? Quand dois-je utiliser IEnumerable et quand IQueryable ?