Expression<Func<Employee, bool>> employeeWhere = R => true;
employeeWhere = R => R.PositionCode == "M";
employeeWhere = R => R.IsActive; //want only ones which are true
以上是否构建了对此的查询:
SELECT * FROM EMPLOYEE
WHERE POSITIONCODE = "M" && IsActive = 1
这就是我要归还的内容
var result = _db.Employees
.Where(employeeWhere)
.Select(p => new { p.EmployeeID, p.FName, p.MName, p.LName })
.AsEnumerable()
.ToDictionary(kvp => kvp.EmployeeID, kvp => kvp.FName + " " + kvp.MName + " " + kvp.LName);
return new SelectList(result, "Key", "Value");
答案 0 :(得分:2)
上面的代码不构建您发布的SQL查询,如果这就是您所要求的。
除非我弄错了,否则你会两次覆盖employeeWhere的价值。我想你需要:
Expression<Func<Employee, bool>> employeeWhere = R => R.PositionCode == "M" && R.IsActive;
如果你有输入参数(例如 bool myBoolParam 和十进制myDecimalParam ),那么你可以这样做:
Expression<Func<Employee, bool>> employeeWhere =
(R => R.PositionCode == "M" && R.IsActive && myBoolParam && R.Salary > myDecmialParam);
答案 1 :(得分:0)
我想你想要这样的东西:
var query = from e in _db.Employees
where e.PositionCode == "M" && e.IsActive
select new { e.EmployeeID,
Value = e.FName + " " + e.MName + " " + e.LName
};
return new SelectList(query.ToDictionary(x => x.EmployeeID, "Key", "Value");