Reusing Linq Queries
Today I was working on a class library(someone else wrote) that used Linq extensively. One of the problems I noticed was, there was no reuse of queries in the class. Every method pretty much return List<T> where T was a custom class that was created by joining three Linq to SQL tables. Here is an example of that.
Basically based on this schema, the developer was returning a custom class called Product Summary which would return data needed fields from all 3 tables. On this class there were quite a few methods and few of them were returning products based on category or supplier. Here is simple implementation of of these methods done by the existing developer.
Looking at the above code, you would notice the same problem that I saw when I first looked at the code. Both methods, GetProdByCat and GetProdBySupp has exactly the same implementation except for the filter they applied. So how could I reuse the query that class doesn't suffer from redundancy. Looking at the code the first time, I realized that he was returning a custom class called ProductSummary which was his own class not Linq table or a class generated by the ormapper so I felt that once you return a custom class, you further cannot apply transformation to it. But that surely is not true. Even when your select is returning a custom business class instead of Linq class, you can apply further transformations to it as long as your class returns an IQueryable<T>. Here is the class converted to reuse queries.
Looking at the above code, you will notice that class is much cleaner and its reusing queries because all the methods return IQueryable on which you can further apply transformation and filters.
Note: To those who think that both my methods GeProdByCat and GetProdBySup are applying filter in memory, this is certainly not the case. Any filter that you apply is being sent to the SQL server for execution.