实体框架将数据检索为列表(字符串)

时间:2012-09-07 16:55:49

标签: entity-framework

我有以下代码从县菜单中选择给定一个stateid的所有县。

 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

我得到查询返回模型列表的任何错误。关于错误所在的任何想法?

1 个答案:

答案 0 :(得分: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),您将构建字符串列表而不是县实体列表。

干杯。

相关问题