... has no supported translation to SQL.

S

shapper

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
 
M

Marc Gravell

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...
 
S

shapper

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...

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.
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top