如何从entityFramework列表中设置复选框值

时间:2013-12-20 14:50:33

标签: c# asp.net linq entity-framework linq-to-sql

我必须根据我从实体框架获得的List中的值设置复选框Checked或Unchecked:

这是我的代码但他们没有工作给我错误“只有赋值,调用,递增,递减,等待和新对象表达式可以用作语句”

foreach (var item in leader)
{
item.IsTopLeader == true ? chkIsTopLeader.Checked = true : chkIsTopLeader.Checked = false;
item.IsSubLeader == true ? chkIsSubLeader.Checked = true : chkIsSubLeader.Checked = false;
item.IsTeacher == true ? chkIsTeacher.Checked = true : chkIsTeacher.Checked = false;
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下命令缩短代码:

foreach (var item in leader)
{
   chkIsTopLeader.Checked = item.IsTopLeader;
   chkIsSubLeader.Checked = item.IsSubLeader;
   chkIsTeacher.Checked = item.IsTeacher;
}

您收到错误是因为您的三元运营商需要分配一些东西 - 即:

bool someString = (1 == 1) ? "yes" : "no";