我有一个简单的问题让我发疯。我试图查询数据实体,一切正常,直到我尝试引用外部的东西。在下面的代码中(完美地工作)我想将.where clientID == 15更改为.where客户端ID是组合框中显示的值,组合框值成员是一个整数:
<root>
<node>Text</node>
</root>
非常感谢任何简单的帮助。
我正在使用带有C#和EF6的VS2015社区
答案 0 :(得分:5)
首先获取值,将其转换为正确的类型,然后在linq查询中使用该参数:
private void cboxcos_SelectionChangeCommitted(object sender, EventArgs e)
{
int value = (int)comboBox1.SelectedValue;
using (var context2 = new tvdm())
{
var cus = context2.tblContacts
.Where(c => c.ClientID == value)
.Select(c => new { c.LastName, c.FirstName, c.JobTitle, c.Telephone, c.Mobile, c.EMail })
.OrderBy(p => p.LastName)
.ToList();
dataGridView1.DataSource = cus;
}
}
答案 1 :(得分:0)
我想你需要像
这样的东西var cus = context2.tblContacts
.Where(c => c.ClientID == Convert.ToInt32(myComboBox.SelectedItem))
.Select(c => new {c.LastName, c.FirstName, c.JobTitle, c.Telephone, c.Mobile, c.EMail })
.OrderBy(p => p.LastName)
.ToList();
答案 2 :(得分:0)
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox senderComboBox = (ComboBox) sender;
int selectedValue = (int)senderComboBox.SelectedValue;
// and assuming that there's nothing wrong with this code
using (var context2 = new tvdm())
{
var cus = context2.tblContacts
.Where(c => c.ClientID == selectedValue)
.Select(c => new { c.LastName, c.FirstName, c.JobTitle, c.Telephone, c.Mobile, c.EMail })
.OrderBy(p => p.LastName)
.ToList();
dataGridView1.DataSource = cus;
}
}
答案 3 :(得分:0)
我实际上最终得到了:
{{1}}