1

I want to convert this 100% working SQL Query into a Linq Query using Vb.net. Thanks

SELECT     
    TOP (100) PERCENT Level3.L2_ID, 
    DATEDIFF(day, Level3.ACT_DATE, BaseLine.ACT_DATE) AS Diff, 
    CASE WHEN datediff(day, Level3.ACT_Date, Baseline.ACT_Date) >0 THEN 1 END AS Green 
FROM Level3 
INNER JOIN BaseLine 
    ON Level3.L3_ID = BaseLine.L3_ID

I first tried the following code in LinqPad (as a vb Expression) and it is working

From l In Level3s
Join a In Baselines 
    On l.L3_ID Equals a.L3_ID
Select 
    Activity = (l.L2_ID) , 
    Diff = (a.ACT_DATE.day-l.Act_Date.day)

but does not work when I add

Green = if (a.ACT_DATE.day-l.Act_Date.day) >= 0, 1 end if

What is the correct syntax for this.

From l In Level3s
Join a In Baselines 
     On l.L3_ID Equals a.L3_ID
Select 
     Activity = (l.L2_ID) , 
     Diff = (a.ACT_DATE.day-l.Act_Date.day),
     Green = if (a.ACT_DATE.day-l.Act_Date.day) >= 0, 1 end if
4

1 回答 1

2

感谢 Adam Rackis 的有用建议;我让它按如下方式工作:

Protected Sub LinqDataSource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource2.Selecting

Dim mylink As New DataClassesDataContext
Dim provider As New NumberFormatInfo()

Dim myresult = From l In mylink.Level3s
               Join a In mylink.BaseLines On l.L3_ID Equals a.L3_ID
               Select Activity = l.L2_ID, Diff = (a.ACT_DATE.Day - l.ACT_DATE.Day), Green = IsGreen(a.ACT_DATE.Day - l.ACT_DATE.Day)
        e.Result = myresult
    End Sub

Function IsGreen(ByVal dayx As Integer) As Integer
        If (dayx > 0) Then
            Return 1
        Else
            Return 0
        End If
    End Function
于 2011-02-19T19:38:12.943 回答