我需要创建一个LINQ语句,该语句使用用户从组合框中选择的国家/地区,并在列表框中显示数据库中的城市。附件就是我到目前为止所拥有的。有许多国家因此使用if语句将不起作用。
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim myQ1 = From X In CustomersDataset.Customers Where
X.Country = ComboBox1.SelectedItem Select X.City
'(X.Country = "France" Select X.City)-only shows city options for France obviously.
lstCity.DataSource = myQ1.ToList
End Sub
答案 0 :(得分:0)
ComboBox SelectedItem属于Object类型,是它的界限。因此,您必须将DataRowView强制转换为String,以便可以在linq查询中进行比较:
Dim strSelectedItemValue As String = CType(ComboBox1.SelectedItem, DataRowView).Item("ReplaceWithTheColumnNameOfTheCountryField")
Dim myQ1 = From X In CustomersDataset.Customers
Where X.Country = strSelectedItemValue
Select X.City
lstCity.DataSource = myQ1.ToList
lstCity.DataBind()