【发布时间】:2019-12-28 10:17:30
【问题描述】:
我有以下代码:
if (groupRadio.Checked)
{
using (gf = new GFilterEntities())
{
dgvFiltered.DataSource = gf.GroupTs.Where(x => x.G_Platform.Equals(platformCombo.Text) &&
x.G_Type.Equals(typeCombo.Text) && x.fieldNameCombo.Text <= how to do this
}
}
我想从 x.fieldNameCombo.Text 中的 ComboBox 动态传递列名,但我无法这样做。如何做到这一点?
示例:G_Platform 和 G_Type 是 GroupT 表中的列名。当我输入 x。我可以看到这些值填充在 Visual Studio 中。
但是,我希望能够使用用户在组合框中输入的值进行过滤。所以类似于 x.fieldNameComboBox 中的用户值.Equals(textBox1.text)。 fieldNameComboBox 值将是一个列名,就像 G_Platform 和 G_Type 一样。
谢谢。
更新 基于下面的 cmets 之一(由 sam 编写),我的代码现在显示为:
private void btnSearch_Click(object sender, EventArgs e)
{
if (groupRadio.Checked)
{
using (gf = new GFilterEntities())
{
var testClass = new TestClass { ComboBoxSelectedText = fieldNameCombo.Text, UserInputValue = txt1.Text};
var comboBoxSelectedColumnValues = from object item in gf.GroupTs
let propertyInfo = item.GetType().GetProperties()
from info in propertyInfo
where string.Equals(info.Name, testClass.ComboBoxSelectedText)
select (string)info.GetValue(item)
into s
where !string.IsNullOrWhiteSpace(s)
select s;
dgvFiltered.DataSource = gf.GroupTs.Where(x => x.G_Platform.Equals(platformCombo.Text) &&
x.G_Type.Equals(typeCombo.Text) && comboBoxSelectedColumnValues.Equals(testClass.UserInputValue)
).ToList();
}
}
}
public class TestClass
{
public string ComboBoxSelectedText { get; set; }
public string UserInputValue { get; set; }
}
我收到一条错误消息: System.NotSupportedException:“无法将类型“Filtering.GroupT”转换为类型“System.Object”。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型。'
【问题讨论】:
-
如何获取用户输入值?你能举一两个例子吗?因为您使用的是
-
您可以在组合框值上执行 switch 或 if-else if 逻辑以确定要包含的过滤器。
-
这是一个 Windows 窗体应用程序。我从表单中的 ComboBox 获取用户输入。当我输入 x。我得到了表中所有列名的列表。如何使用 ComboBox 中的文本来获取列名,而不是从我从 x 获得的下拉列表中选择。
-
你和表单在同一个班级里跑吗?要获得组合框,您需要使用表单的实例。您可能还需要进入 design.cs 文件并公开变量。
-
@jdweng 是的。我和表格在同一个班级里跑步。问题是从上面组合框中的用户输入动态获取列属性(我添加了图片),而不是从我得到的列表中预先选择(还添加了上面的代码图片)
标签: c# datagridview