Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method X has no supported translation to SQL - booleans and datetime

Tags:

c#

linq-to-sql

Has anyone a suggestion of how to make this function supported by LINQ to SQL?

public bool IsEnabled()
{
    return !this.Disabled && 
           ((!this.EnabledFrom.HasValue || this.EnabledFrom < DateTime.Now) && 
            (!this.EnabledTo.HasValue || this.EnabledTo > DateTime.Now));
}

Disabled is a bool, EnabledFrom and EnabledTo is DateTime? and all database fields.

like image 834
Eilev Avatar asked Nov 03 '11 13:11

Eilev


1 Answers

Make your IsEnabled method return an expression.

See here: http://www.atrevido.net/blog/2007/09/05/Calling+Custom+Methods+In+LINQtoSQL.aspx

Something like below (untested):

static Expression<Func<Account, bool>> IsEnabled = a =>
    !a.Disabled && 
    ((!a.EnabledFrom.HasValue || a.EnabledFrom < DateTime.Now) && 
     (!a.EnabledTo.HasValue || a.EnabledTo > DateTime.Now));
like image 107
George Duckett Avatar answered Nov 03 '22 11:11

George Duckett