I am using the Entity Framework and Linq to Entities. I have created a small database pattern & framework to implement versioning as well as localization. Every entity now consists of two or three tables, (ie Product, ProductBase & ProductLocal).
My linq always includes the following boilerplate code:
from o in DB.Product
from b in o.Base
from l in o.Local
WHERE o.VersionStatus == (int)VersionStatus.Active
&& b.VersionStatus == (int)VersionStatus.Active
&& l.VersionStatus == (int)VersionStatus.Active
&& l.VersionLanguage == Context.CurrentLanguage
select new ProductInstance { Instance = o, Base = b, Local = l }
What I would like to accomplish is to turn the above into:
(from o in DB.Product
from b in o.Base
from l in o.Local
select new ProductInstance { Instance = o, Base = b, Local = l }).IsActive()
Or at worst, something like:
from o in DB.Product.Active()
from b in o.Base.Active()
from l in o.Local.Active()
select new ProductInstance { Instance = o, Base = b, Local = l }
I have extended the base classes the EDM generates to implement some interfaces that enforce the properties ( IVersionStatus and/or IVersionLanguage ). Is there some way I can walk the expression tree, check if the type in the expression implements that interface, then set the VersionStatus accordingly?
I woud love it to be as simple as the first option, just less to write and/or forget. I have seen examples that do it after the fact, after its IEnumerable, but I would rather not pull more from the database than I need to.
Thanks for any tips!