Should I return IEnumerable or IQueryable from my DAL?

后端 未结 4 735
眼角桃花
眼角桃花 2020-12-22 23:52

I know this could be opinion, but I\'m looking for best practices.

As I understand, IQueryable implements IEnumerable, so

4条回答
  •  不思量自难忘°
    2020-12-23 00:30

    HUUUUGGGE difference. I see this quite a bit.

    You build up an IQueryable before it hits the database. The IQueryable only hits the DB once an eager function is called (.ToList() for example) or you actually try to pull values out. IQueryable = lazy.

    An IEnumerable will execute your lambda against the DB right away. IEnumerable = eager.

    As for which to use with the Repository pattern, I believe it's eager. I usually see ILists being passed but someone else will need to iron that out for you. EDIT - You usually see IEnumerable instead of IQueryable because you don't want layers past your Repository A) determining when the database hit will happen or B) Adding any logic to the joins outside the Repository

    There is a very good LINQ video that I enjoy a lot- it hits more than just IEnumerable v IQueryable, but it really has some fantastic insight.

    http://channel9.msdn.com/posts/matthijs/LINQ-Tips-Tricks-and-Optimizations-by-Scott-Allen/

提交回复
热议问题