0

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?

2
  • How do you plan to "convert" a country to a string? You need to select one of its string properties: Where c.stateId = 7 Select c.Name etc. Or return the whole country list GetCountiesfromState(statename As String) As List(Of Country) Commented Sep 7, 2012 at 17:08
  • Good point. I changed the return to list(of county) but my drop down im binding to has nothing in it. I set the list as the dataource and databind it. Commented Sep 7, 2012 at 17:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.