Kevin
Kevin

Reputation: 465

using linq to select a column from a datatable

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

Upvotes: 1

Views: 24551

Answers (1)

dan radu
dan radu

Reputation: 2782

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()

Upvotes: 3

Related Questions