.Net Entity Framework - 让关键字具有条件

时间:2011-07-07 21:24:11

标签: .net vb.net linq entity-framework

我需要在查询中创建一个if-else语句,这意味着如果type = Cash,我只想将OpenAmount添加到ExpectedAmount,问题是它只给了我现金行而不是另一行。

 Dim TenderList = (From t In EnData.BatchRecs 
                   Join b In EnData.RegShifts 
                   On t.BatchID Equals b.RegShiftID 
                   Where t.BatchID = ID 
                   Let ExpectedAmount = (t.ExpectedAmount + b.OpeningAmount) 
                   Where t.TenderName = "CASH"
                   Select t.BatchRecID, ExpectedAmount, t.ExpectedCount, 
                      t.PickUpAmount, t.TenderName, OverShort, 
                      t.PickUpCount, t.TenderID)

可以这样做吗?

1 个答案:

答案 0 :(得分:2)

您应该能够使用条件语句。

Let ExpectedAmount = If(t.TenderName = "CASH",
                        t.ExpectedAmount + b.OpeningAmount,
                        t.ExpectedAmount) 

如果你这样做,你也不需要Where t.TenderName = "CASH"