Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

... has no supported translation to SQL.

10 views
Skip to first unread message

shapper

unread,
Oct 20, 2008, 6:22:29 AM10/20/08
to
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?

Thanks,
Miguel

Marc Gravell

unread,
Oct 20, 2008, 6:37:06 AM10/20/08
to
lol! OK, in this case, stick to null-coalescing (??) ;-p

Fair cop - but it would have worked with LINQ-to-Objects...

Sorry for any confusion...

shapper

unread,
Oct 20, 2008, 9:26:14 AM10/20/08
to

You mean:

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

I still get the same error. I don't think that's the problem.


Marc Gravell

unread,
Oct 20, 2008, 10:29:58 AM10/20/08
to
Ah, right - I thought the GetValueOrDefault was the issue. Indeed, you
can't use a regular method (CheckPostDate) in LINQ, *unless* that method
can be mapped to a UDF at the database. In which case, you need to mark
the method with the [FunctionAttribute], supplying the UDF (etc) and
marking the function as "composable".

Here's a trivial example:

[Function(Name="NEWID", IsComposable=true)]
public Guid Random()
{ // to prove not used by our C# code...
throw new NotImplementedException();
}

At the DB this will become TSQL calling NEWID() - but you can have
parameters etc as you need.

Noet that this only works with some LINQ providers (I belive LINQ-to-SQL
supports it, EF doesn't), and the method needs to be on the data-context.

Marc

0 new messages