I have created some extra functionality on my Linq-to-SQL classes to make things easier as I develop my applications. For example I have defined a property that retrieves active contracts from a list of contracts. However if I try to use this property in a lambda expression or in general in a query it either throws an exception that there is no SQL statement matching that property or it generates one query per item (= a lot of roundtrips to the server).
The queries themselves are not overly complex f.ex:
var activeContracts = customer.Contracts.Where(w => w.ContractEndDate == null);
Whereas I would like it to read as:
var activeContracts = customer.ActiveContracts;
The main reason for me doing this is because it will minimize logical errors on my part and if I in the future want to change what defines an active contract I don't have to redo a lot of code.
Is there a way to specify on a property what SQL it should genereate. Or is there a way to make sure it is usable in a query like below?
var singleContractCustomers = db.Customers.Where(w => w.ActiveContracts.Count() == 1);