Creating new columns in a Select Linq Query based on logical tests
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
Thanks Adam Rackis for the useful suggestion; I got it to work as follows:
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
精彩评论