Priyanshu Gupta
Priyanshu Gupta

Reputation: 1

Why Dbset implement IEnumerable when it already implements IQueryable?

Dbset seems to implement IEnumerable when it already has IQueryable, doesn't this makes the implementation of IEnum redundant as IQueryable already implements IEnum.

Upvotes: 0

Views: 458

Answers (2)

Andrew Shepherd
Andrew Shepherd

Reputation: 45222

First of all, we should correct your terminology:

IQueryable already implements IEnum.

IQueryable doesn't implement anything. It inherits IEnum, which means that every class that implements IQueryable must implement the members of IEnum as well as the members of IQueryable.

I think your question actually is this: why is DbSet defined as implementing IEnum, when it implements IQueryable so it has to implement IEnum anyway.

Technically, stating that DbSet implements IEnum is redundant.

Which leaves the question, Why would anyone unnecessarily state a base interface?

I'll quote this blog article by Eric Lippert: https://blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces/

Perhaps because they believe that doing so makes the code easier to understand and more self-documenting.

Or, perhaps the developer wrote the code as

interface I1 {}

interface I2 {}

interface I3 : I1, I2 {}

and the realized, oh, wait a minute, I2 should inherit from I1. Why should making that edit then require the developer to go back and change the declaration of I3 to not contain explicit mention of I1? I see no reason to force developers to remove redundant information.

Upvotes: 1

behzad besharati
behzad besharati

Reputation: 6910

Sometimes you need all rows of a specific table without any additional where clause, in IEnumerable format. in these circumstances, you can directly cast the DbSet to IEnumerable.

Upvotes: 0

Related Questions