此代码中的chklstDepartment'具有多个值但显示但仅是第一个选定项目的绑定数据。 如何获取所有选定复选框列表值的数据?
List<LessonLearnDetails> objLessonDetails = objLessonDashboard.getLessonLearntDetails();
var searchData = objLessonDetails
.Where(i => ddlAsset.SelectedValue == "0" ||
i._Asset.AssetID == ddlAsset.SelectedValue)
.Where(i => ddlAuditType.SelectedValue == "0" ||
i._Audit.AuditTypeID == ddlAuditType.SelectedValue)
.Where(i => chklstDepartment.SelectedValue == "0" ||
i._Department.DepartmentID == chklstDepartment.SelectedValue)
.Where(i => ddlStartYear.SelectedItem.Text == "--Select All--" ||
i._Year.StartYear == ddlStartYear.SelectedItem.Text)
.Where(i => ddlEndYear.SelectedItem.Text == "--Select All--" ||
i._Year.EndYear == ddlEndYear.SelectedItem.Text)
.Distinct()
.ToList();
BindGrid(searchData);
我还使用了一个函数来存储已检查的值,并使用chklstDepartment.Contains()传递该值,但它返回0。
for (int i = 0; i < chklstDepartment.Items.Count; i++)
{
if (chklstDepartment.Items[i].Selected)
{
chbstr += chklstDepartment.Items[i].Value + ",";
count++;
//if(count > 1)
}
var data = new string[] {chbstr};
}
chbstr = chbstr == string.Empty ? "0" : chbstr;
//chbstr = chklstDepartment.SelectedValue;
//chbstr = chbstr.Split(',').ToString();
return chbstr;
答案 0 :(得分:1)
你不应该返回一个字符串,而是一个可枚举的字符串。你可以改变你的where声明。
类似的东西:
.Where(i => (chklstDepartment.SelectedValue == "0")
|| (chklstDepartment.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Value)
.Contains(i._Department.DepartmentID)))
如果DepartmentID
是int
,您可以尝试:
.Where(i => (chklstDepartment.SelectedValue == "0")
|| (chklstDepartment.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => Convert.ToInt32(item.Value))
.Contains(i._Department.DepartmentID)))
您可以考虑缓存选择:
var selectedIds = chklstDepartment.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => Convert.ToInt32(item.Value))
.ToArray(); // or ToList() or HashSet
.Where(i => (chklstDepartment.SelectedValue == "0")
|| (selectedIds.Contains(i._Department.DepartmentID)))