[英]How can I access all values from a specific column in tables using linq and Entity Framework?
Can someone help me?有人能帮我吗? I want to display all values from a specific column in a combo box using linq.我想使用 linq 在组合框中显示来自特定列的所有值。 I try to do it but I can't figure it out.我试着去做,但我想不通。 I am new to linq and can't find a way to do it我是 linq 的新手,找不到办法
If you want to show a list of string product names and be able to retrieve their int id you can for example:如果您想显示字符串产品名称列表并能够检索它们的 int id,您可以例如:
combo.DisplayMember = "DisplayColumnName";
combo.ValueMember = "IdColumnName";
combo.DataSource = context.TableName.Select(r =>
new KeyValuePair<int, string>(r.IdColumnName, r.DisplayColumnName)).ToList();
You need to replace TableName, DisplayColumnName and IdColumnName with your actual values.您需要将 TableName、DisplayColumnName 和 IdColumnName 替换为您的实际值。
To get the ID of what the user selected (eg for further db queries) you can:要获取用户选择的 ID(例如,用于进一步的数据库查询),您可以:
var id = (int)combo.SelectedValue;
If you want the string of what the user selected:如果您想要用户选择的字符串:
var s = ((KeyValuePair)combo.SelectedItem).Value;
this works.这行得通。
var context = new LoginTest();
var login = context.SignUps
.Select(c => new { c.id, c.Name });
comboBox1.DataSource = login.ToList();
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "Name";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.