Linq is so easy to extend. But the right way to do it is to use and rely on existing infrastructure. Let’s take as an example Duplicate extension that return all duplicates from a collection.
One example is here. For convenience I’m copying the code here.
Yes, this code works but only in-memory. It will not work in the Database (it will not generate SQL, but will be evaluated in the memory instead).
Let’s compare this solution to the one below:
This is much easier to implement (actually one line of code) and is based on existing Linq infrastructure. I believe it might be optimised, but you get the points.
- Don’t hurry to invent the wheel.
- Use standard Linq convention with IQueryable interface whenever possible.