我有一个简单的场景。我想列出除登录用户之外的所有员工。
类似的SQL条件是
select * from employee where id not in(_loggedUserId)
如何使用 LINQ 实现上述目标。我尝试了以下查询但未获得所需的列表
int _loggedUserId = Convert.ToInt32(Session["LoggedUserId"]);
List<int> _empIds = _cmn.GetEmployeeCenterWise(_loggedUserId)
.Select(e => e.Id)
.Except(_loggedUserId)
.ToList();
最佳答案
Except
期望类型为 IEnumerable<T>
的参数, 不是 T
, 所以它应该是这样的
_empIds = _cmn.GetEmployeeCenterWise(_loggedUserId)
.Select(e => e.Id)
.Except(new[] {_loggedUserId})
.ToList();
另请注意,在排除列表仅包含一项并且可以替换为类似 .Where(x => x != _loggedUserId)
的情况下,这确实是多余的
关于c# - 在 Linq 中不符合条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175687/