1

I have a service call that is returning the date and time as strings from a SAP web service and I am trying to parse those strings into a datetime object in the local model.

So far I have stubbed out the method but the first thing I noticed was the compiler complaining that it could not convert the date as its not a delegate type.

Error

Error 1 Lambda expression cannot be converted to 'Date' because 'Date' is not a delegate type. C:\Users\phil.murray\Desktop\SAP Orders Test\WindowsApplication1\Form1.vb 43 57 WindowsApplication1

Code

    _ordersProxy.Z_SPIN_ORDERS(_header, _order, _code)

    Return From h In header.OrderBy(Function(w) w.AUFNR)
                Select New Order With {.Aufnr = h.AUFNR, .BatchType = h.BATCH_TYPE,
                                       .EngineType = h.ENGINE_TYPE, .ProgrammedQuantity = h.PROGRAMMED_QTY,
                                       .StartDate = Function()

                                                        Dim startDate As DateTime
                                                        Dim startTime As DateTime

                                                        Date.TryParse(h.START_DATE, startDate)
                                                        Date.TryParse(h.START_TIME, startTime)


                                                        Return New DateTime(startDate.Year, startDate.Month, startDate.Day,
                                                                            startTime.Hour, startTime.Minute, startTime.Second)

                                                    End Function}

Local Model

Public Class Order

    Public Property Aufnr As String
    Public Property EngineType As String
    Public Property ProgrammedQuantity As Int16
    Public Property BatchType As String
    Public Property StartDate As DateTime

    Public Property OrderParts As IList(Of Part)
    Public Property OrderProcessCodes As IList(Of ProcessCode)

End Class

What am I missing when parsing this string with a lambda expression?

1 Answer 1

1

The problem is that StartDate is presumably a property of type DateTime - whereas your lambda expression would be assignable to a Func(Of DateTime).

You could call the lambda expression to set StartDate, but fundamentally if a property is of type DateTime, you need to give it a value of that type, not just a function which can be executed to obtain a value.

Of course, another alternative (potentially) is to change the StartDate property type. It's unclear exactly what you're trying to do at the moment, or what kind of object you're setting this property on.

Sign up to request clarification or add additional context in comments.

9 Comments

I have undated my code to show the entire call and the local model I am loading.
@PhilMurray: Right, but we don't know why you're trying to use a lambda expression in the first place. Are you trying to defer the execution? If not, why not just execute the code before you start constructing the object?
So if I understand correctly a Lambda expression returns a Func(of T) and not the value. No I see why its not working. Is there a way to invoke the delegate method while building the object in LINQ?
@PhilMurray: Well you could probably cast the lambda expression to a Func(Of DateTime) and then just call it as you would any other delegate. But why bother? Why not just either compute it first, or extract that code into a method you can call? This just doesn't feel like a natural situation for using a lambda expression to me.
So you would recommend changing the code to something like ... .StartDate = ParseTime(h.START_DATE, h.START_TIME)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.