vb.net 使用 linq 从数据表中选择一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14637167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:38:37  来源:igfitidea点击:

using linq to select a column from a datatable

vb.netlinqdatatable

提问by Kevin

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

目前我正在使用 sql 并点击数据库来填充下拉列表。

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

DB 是一个自定义类,sql 返回一个数据表。我想在已经有 IMLOCN 的数据表上使用 linq

 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") _ DistinctI'd like to be able to use linq and I'd like to learn more about it

我已经尝试过 dt.AsEnumerable()Dim location = From u In dt.AsEnumerable _ Select u.Field(Of String)("IMLOCN") _ Distinct我希望能够使用 linq,我想了解更多关于它的信息

回答by dan radu

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