0

Recently we have started using sonarLint plugin in visual studio for improving code quality and coverage. After that we are getting the suggestion from replacing the Tolist() with ASEnumerable.

Error text: Drop this useless call to 'ToList' or replace it by 'AsEnumerable' if you are using LINQ to Entities.

The object for which it is giving the exception is fetch using the dapper and datatype of it is IEnumaerable.

  • `AsEnumerable` is a [simple cast](https://stackoverflow.com/q/2013846/1997232), while `ToList` will create `List` in memory. Previous operation is something what just need a cast, it's already `IEnumerable` and not deffered (must be not defferred). So unless you want to use `List` functionality (e.g. adding/removing items) `ToList` call is redundant. – Sinatr May 08 '20 at 07:41

2 Answers2

0

ToList() and AsEnumberable() has potential performance differences. You may find following link useful

https://stackoverflow.com/a/3628705/13049026

0

If you are only need to process each item one at a time, AsEnumerable will allow you to process each item while the query results are being returned. Allowing the garbage collector to remove objects from memory before you have finished, which might reduce your peak memory use.

Will that result in a noticeable performance improvement? That depends on lots of other factors.

Jeremy Lakeman
  • 9,515
  • 25
  • 29