2012-09-07 50 views
0

我有以下代碼來選擇縣縣表給出的所有縣的狀態ID。實體框架檢索數據作爲列表(字符串)

Public Shared Function GetCountiesfromState(statename As String) As List(Of String) 
    Dim context As New Model.teckEntities() 
    Dim query = From c In context.counties Where c.stateId = 7 Select c 
    Return query.ToList() 
End Function 

我得到任何錯誤,查詢返回模型列表。關於錯誤在哪裏的任何想法?

+0

你打算如何將一個國家「轉換」爲一個字符串?您需要選擇其中一個字符串屬性:'Where c.stateId = 7選擇c.Name'等。或者返回整個國家列表'GetCountiesfromState(statename As String)As List(Of Country)' – nemesv

+0

好點。我改變了列表(縣)的列表,但是我的下拉列表中沒有任何內容。我將列表設置爲數據源並對其進行數據綁定。 – dinotom

回答

1

如果在縣實體Name(或Title)字段,它應該是這麼簡單:

Public Shared Function GetCountiesfromState(statename As String) As List(Of String) 
    Dim context As New Model.teckEntities() 
    ' Here is the difference: 
    Dim query = From c In context.counties Where c.stateId = 7 Select c.Name 
    Return query.ToList() 
End Function 

在你上面的代碼你選擇c這是一個縣的實體,而不是必須是字符串屬性。通過選擇c.Name(或c.Title),您將建立一個字符串列表,而不是縣實體列表。

乾杯。

相關問題