Is it possible to create custom methods in entity classes in linq to sql

Posted by doekman on Stack Overflow See other posts from Stack Overflow or by doekman
Published on 2010-06-10T08:22:52Z Indexed on 2010/06/10 10:23 UTC
Read the original article Hit count: 211

I have a table Site in SQL with (amongst others) three properties idReviewer1, idReviewer2, idReviewer3. Now I would like to create a methods on the entity class of Site to check if a user is a reviewer:

partial class Site
{
    public bool IsReviewer(int idUser)
    {
        return idReviewer1 == idUser || idReviewer2 == idUser || idReviewer3 == idUser;
    }
}

and I use it like this:

return from s in db.Sites
       where s.IsReviewer(user)
       select s;

However, Linq to SQL doesn't know how to translate this to SQL. I get the following error message:

Method 'Boolean IsReviewer(Int32)' has no supported translation to SQL.

I'd rather not write this:

return from s in db.Sites
       where idReviewer1 == idUser || idReviewer2 == idUser || idReviewer3 == idUser
       select s;

Is there any way to put this functionality in one place, without resorting to SQL?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about visual-studio-2008