基于逻辑测试在Select Linq Query中创建新列

时间:2011-02-19 16:44:09

标签: linq vb.net-2010

我想使用Vb.net将这个100%工作的SQL查询转换为Linq查询。感谢

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

我首先在LinqPad中尝试了以下代码(作为vb表达式)并且它正在运行

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

这是什么语法。

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

1 个答案:

答案 0 :(得分: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