简体   繁体   English

如何使用 linq 和实体框架访问表中特定列的所有值?

[英]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.

相关问题 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? 如何从Entity Framework / LINQ中的特定表中提取元组列表? - How can I extract a list of Tuple from a specific table with Entity Framework / LINQ? 使用实体框架,如何创建查询以从数据库中获取所有表的列名 - Using Entity Framework, how to create a query which will fetch the column names of all tables from a database 如何使用 Entity Framework C# 获取与特定表相关的表名 - How I can get the name of tables has relation with specific table using Entity Framework C# 如何通过Entity Framework验证表之间的所有关系? - how I can verify all the releations between tables by Entity Framework? 在实体框架中,我可以创建一个计算列来计算每一行的所有值吗? - In Entity Framework can I create a computed column that calculates all the values from every row? 如何使用LINQ和Entity Framework对总计进行分组? - How can I do a group by with totals using LINQ and Entity Framework? 从数据库获取所有表,并使用C#,Linq查询和Entity Framework遍历每个表 - Get all tables from database and loop through each table using C#, Linq query and Entity Framework 如何使用 LINQ 连接多个表,包括连接表(由实体框架自动生成) - How can I join multiple tables including a join-table (auto-generated by Entity Framework) using LINQ 使用 LINQ 和实体框架,我怎样才能用数据库中的数据填充 Dbo 的一些属性? - Using LINQ and Entity Framework, how can I just populate some properties of a Dbo with data from the DB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM