What is the difference between IQueryable and IEnumerable?
Difference Between IEnumerable and IQueryable
President Trump has an employee: Freddie Frodo.
“Excuse me!” demands Trump, “I want the records of all Mexicans who voted against me in the election, who also live in the white house, and who refuse to pay for my wall.”
“Yes sir” was the demure reply.
So off the young intern went to the Electoral Office. He then obtained the names, addresses of every single vote, and brought it back to the white house. It was bedlam – as you can imagine. And Trump walks in, in the midst of this chaos and mess and asks:
“What do you think you are doing?”
“Sir I’m sifting through the records, so I can give back only those people who voted against you.”
Trump was livid:
“Yes but why did you collect all the records and bring them back here to sort out? Why didn’t you sort everything out at the Electoral office, and bring home only the records that you need?
Well – the simple answer is that the intern was not implementing the IQueryable interface. IQueryable allows you to return records which are filtered. Where as IEnumerable returns all the records and forces you to filter only when you already have them.
In other words, the intern was using IEnumerable instead of IQueryable, raising the ire of the president elect.
……And that’s the basic difference.