Returning IEnumerable vs. IQueryable

前端 未结 14 2748
梦毁少年i
梦毁少年i 2020-11-21 22:59

What is the difference between returning IQueryable vs. IEnumerable, when should one be preferred over the other?



        
14条回答
  •  耶瑟儿~
    2020-11-21 23:45

    Both will give you deferred execution, yes.

    As for which is preferred over the other, it depends on what your underlying datasource is.

    Returning an IEnumerable will automatically force the runtime to use LINQ to Objects to query your collection.

    Returning an IQueryable (which implements IEnumerable, by the way) provides the extra functionality to translate your query into something that might perform better on the underlying source (LINQ to SQL, LINQ to XML, etc.).

提交回复
热议问题