15

我在这里有一点误解为什么我在这里有一个错误我需要解析它这段代码有什么问题?

UberTrackerEntities ctx = UberFactory.Context;
IEnumerable<HtUser> users = HtUser.GetAll();
string selectedBU = rcbBusinessUnits.SelectedValue;
string selectedDepartment = rcbDepartment.SelectedValue;

HtDepartment department = ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId ==selectedDepartment);

if (department != null) 
{
    users = users.Where(u => u.HtDepartments.Contains(department));
}

感谢您的帮助和快速答复!

PS:我觉得我刚刚结束了它接缝只是一个愚蠢的小错误......

4

3 回答 3

15

selectedDepartment在 LINQ 查询中进行比较之前,您需要将其转换为整数。

int selectedDepartment = Convert.ToInt32(rcbDepartment.SelectedValue);

在您的查询中:

ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId == selectedDepartment);

d.DepartmentId是 int 类型,selectedDepartment而是字符串,您可以使用运算符比较两者==

于 2013-03-14T09:05:50.957 回答
0

d.DepartmentId是一个int并且selectedDepartment是一个字符串。

您将需要使用Int32.Parse,Int32.TryParseConvert.ToInt32

编辑:

int selectedDepartmentId = Convert.ToInt32(selectedDepartment);

HtDepartment department = ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId == selectedDepartmentId));
于 2013-03-14T09:06:14.060 回答
0

您选择的部门是字符串类型,您的 id 是 int 类型。您应该将您的 selectedDepartment 转换为 int:

int selectedDepartment = Convert.ToInt32(rcbDepartment.SelectedValue);
于 2013-03-14T09:06:26.283 回答