1

Currently I am using sql and hitting the database to populate a dropdown.

Dim sqlStatement = "SELECT DISTINCT IMLOCN FROM table order by IMLOCN desc"
    LocationDropDown.DataSource = DB.sql(dbSalesWeb, sqlStatement)
    LocationDropDown.DataTextField = "IMLOCN"
    LocationDropDown.DataBind()
    LocationDropDown.Items.Insert(0, "ALL")

DB is a custom class and sql returns a Datatable. I'd like to use linq on a datatable that already has the IMLOCN

 Protected Sub updateDropDowns(ByVal dt As DataTable)


    Dim location = From u In dt.Rows _
                        Select u("IMLOCN") _
                        Distinct

    LocationDropDown.DataSource = location.ToList
    LocationDropDown.DataBind()

End Sub

I've tried dt.AsEnumerable() and Dim location = From u In dt.AsEnumerable _ Select u.Field(Of String)("IMLOCN") _ Distinct I'd like to be able to use linq and I'd like to learn more about it

1
  • What's your question? Are you getting an error? Commented Feb 1, 2013 at 0:19

1 Answer 1

3

Not sure what error you get, but this query should work fine:

Dim qLocation = (From u In dt.AsEnumerable() _
                Select u.Field(Of String)("IMLOCN")).Distinct()
LocationDropDown.DataSource = qLocation.ToList()
LocationDropDown.DataBind()
Sign up to request clarification or add additional context in comments.

3 Comments

with the above linq query I get the error " unable to cast object of type System.Byte to type System.string" In the DB IMLOCN is a tiny int. when I look in the debugger Location doesn't have any results in it. If I switch the column to IMSTK which is a varchar in the DB Location actually has results in it, but I get the error "dataBinding System.String does not contain a property with the name IMLOCN" but I switched and used IMSTK instead of IMLOCN, why does the debugger say IMLOCN still?
I databind the dropdown when the page is first loaded to the results from a sql query. Is that causing problems with databinding it to the linq results?
When I databind to the sql results earlier I am also setting the datatextfield = "IMLOCN" is that causing the string does not contain a property with the name IMLOCN error?

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.