views:

32

answers:

1

I have this one line property

Public ReadOnly Property DateToUniversal(ByVal dt As Nullable(Of Date)) As String
    Get
        Return If(dt.HasValue, dt.Value.ToString("u").Substring(0, 10), "")
    End Get
End Property

if i'm trying to use that into a

from n in mydatacontext 
select new myObj {n.field1,n.field2, DateToUniversal(n.field3)}

it doesn't work all the time because LINQ2SQL cannot convert that into a sql query.

is there a way to do this?

+1  A: 

You'll have to turn it into two queries:

var query = (from n in mydatacontext select new {n.field1,n.field2, n.field3}).AsEnumerable();

query = from n in query select new {n.field1, n.field2, DateToUniversal(n.field3)};

It's important that query be IEnumerable instead of var (or IQueryable)It's important that you call AsEnumerable() so that you get back an IEnumerable<T>, as we want to force the second query to use LINQ to Objects on the results of the first (LINQ to SQL) query.

I'm not in front of VS at this time, but this should do the trick.


Thanks to itowlson for pointing out that it has to be IEnumerable<T>, not just IEnumerable!

Adam Robinson
@itowlson: You're absolutely right, thanks for that. Edited!
Adam Robinson