Why use AsQueryable() instead of List()?

后端 未结 5 1652
傲寒
傲寒 2020-11-28 02:49

I\'m getting into using the Repository Pattern for data access with the Entity Framework and LINQ as the underpinning of implementation of the non-Test Repository. Most samp

5条回答
  •  眼角桃花
    2020-11-28 03:29

    AsQueryable is an extension method for IEnumerable that could do two things:

    • If the IEnumerable implements IQueryable justs casts, doing nothing.
    • Otherwise creates a 'fake' IEnumerable (EnumerableQuery) that implements every method compiling the lambdas and calling to Enumerable extension methods.

    So in most of the cases using AsQueryable is useless, unless u are forced to pass a IQueryable to a method and u have a IEnumerable instead, it's a hack.

    NOTE: AsQueryable is a hack, IQueryable of course is not!

提交回复
热议问题