entity framework retrieve data as list(of string)

I have the following code to select all counties given a stateid from a counties table.

 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

I get any error that query is returning a list of model. any thoughts on where the error lies?

Answers


If there is a Name (or Title) field on the County entity, it should be as simple as:

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

In your code above you were selecting the c which, is a County entity, not necessarily a string property. By selecting c.Name (or c.Title) instead, you'll be building a list of strings instead of a list of county entities.

Cheers.


Need Your Help

Neo4j - Faster to rely on relationships or to duplicate/store data of objects I'm referencing?

neo4j

I'm new to Neo4j and still experimenting / changing up my whole understanding of building databases with it.

Ember deleting objects using em methods

javascript ember.js ember-data

Hai once again ive deleting action to take care of.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.