I am working on a repository pattern where the API look as follows:
var visitor = repository.Find(x => x.EmailAddress == credentials.EmailAddress &&
x.Password == credentials.Password);
where visitor is a domain object and x represents this domain object. The method signature of the Find method on the repository is:
T Find(Func<T, bool> query);
This is all wonderful until I attempt to use this with Linq2Sql because linq2sql creates its own objects and as a result when I want to call:
context.visitors.FirstOrDefault(query);
there is a type mismatch because linq2sql expects a function of the type it created and not the function I am passing in.
Well to start with you’ll need to change your
Findsignature to:LINQ to SQL needs to have the logic as an expression tree instead of a plain delegate, otherwise it can’t work out how to translate it into SQL.
Beyond that, I’m afraid it’s not terribly clear – it sounds like you’re not using the same domain classes for your repository and LINQ to SQL. Is that right? That sounds like a potential problem; at the very least it’s going to make life pretty tricky.