C#:LINQクエリからComboBox ValueMemberを設定する方法
-
14-11-2019 - |
質問
OK SO COMBOBOXのデータソースがLINQクエリの結果です。
//load QA names
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
.
問題IMは、次のコードの行を追加しようとしているとき
cboQASupervisor.ValueMember = "ID";
.
ランタイムでエラーが発生します。これを修正するにはどうすればいいですか?
補正: エラーは次のとおりです。
新しい値メンバーにバインドできません。 パラメータ名:value
解決
You specify ID as the value field, but you don't have ID property in your anonymous type.
Assuming you have ID in your LUT_Employees object:
var qaNames = (
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a.ID, Names = a.lastName + ", " + a.firstName })
.ToList();
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "ID";
他のヒント
You can try this:
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { Id = a.ID, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames.ToList();
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "Id";
Add .ToList()
to your code in the datasource line.
所属していません StackOverflow