2012-08-16 38 views
1

我想從我的數據庫中的一個整數傳回給我,如下(我知道這將是唯一的)一個整數:充分利用數據庫鑄錯誤

Dim SequenceyQuery = From a In db.ServiceSequences 
          Where (a.ServiceId = ReqService And a.StationId = AtStation) 
          Select a.Sequence 

    Dim TempSequence As Integer = CInt(SequenceyQuery.ToString) 

但我得到的問題嘗試投放它在第二行代碼中。這是從這裏得到這個整數的最好方法,以及如何將它作爲一個整數?

回答

0

您不應該在您的查詢中致電ToString()

改爲調用First(),它將返回第一個元素。

Dim TempSequence As Integer = SequenceyQuery.First() 
0

使用FirestOrDefault()

Dim SequenceyQuery = From a In db.ServiceSequences 
        Where (a.ServiceId = ReqService And a.StationId = AtStation) 
        .FirstOrDefault() 

Dim TempSequence As Integer = SequenceyQuery.Sequence 
0

試試這個:

Dim SequenceyQuery = (From a In db.ServiceSequences 
         Where (a.ServiceId = ReqService And a.StationId = AtStation) 
         Select a.Sequence).FirstOrDefault() 

或U可以先用()代替