2012-09-07 40 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),您将建立一个字符串列表,而不是县实体列表。

干杯。

相关问题