I find myself repeating business-riles too much in my LinqToEntities when querying. That's not good. ;)
Say I have two tables:
Member
- Id
- Name
MemberShip
- Id
- MemberId (fk to member.Id)
- StartDate
- ExpirationDate
- IsCancelled
Now I define a valid membership as:
- Now is between StartDate and ExpirationDate
- IsCancelled is false
So now I would write functions in repositories like...:
- GetActiveMemberships
- GetMembersWithActiveMemberships
- MemberHasActiveMembership
- (etc)
...So in all these functions, that use LinqToEntities, I have code like..:
...
And membership.IsCancelled = 0 _
And membership.StartDate < Now() _
And membership.ExpirationDate > Now() _
...
What is the best way to avoid repeating this in every single Linq-to-entities query?
Can I separate my definition of a valid membership and apply that in other functions?