... has no supported translation to SQL. RRS feed

  • Question

  • Hello,

    I am creating a int value has follows:

    int result = db.Posts.Where(p => p.IsPublished == true && CheckPostDate(s.CreatedAt).GetValueOrDefault(false) == true).Count();

    I get the error:
    Method 'System.Nullable`1[System.Boolean] CheckPostDate(System.Nullable`1[System.DateTime])' has no supported  translation to SQL.

    Can't I call a function I created in a Linq query?

    How can I solve this problem?

    Monday, October 20, 2008 1:28 PM


  • The problem is that LINQ to SQL has no way of being able to translate your method into an SQL statement to send to the database server.

    One option is to split the query into two parts, one LINQ to SQL, one LINQ to Objects (which will handle calling a method) which in the following method is achieved with ToList() causing the LINQ to SQL part to execute and provide a list of objects first.

    int result = (db.Posts.Where(p => p.IsPublished).ToList()).Where(CheckPostDate(s.CreatedAt).GetValueOrDefault(false)).Count();

    Monday, October 20, 2008 3:15 PM