【问题标题】:Convert SQL Statement to Linq将 SQL 语句转换为 Linq
【发布时间】:2023-03-15 17:50:02
【问题描述】:

这是我的示例查询

Select id from employee where id in
(select employeeid from employeecenter where CenterCodeId in 
(select CenterCodeId from employeecenter where employeeid=40))

我们如何使用 Linq 实现上述目标

//Gets all the centerCodeIds allotted to an employee
Session["LoggedUserId"] = 40;
List<int> _centerCodeIds = _cmn.GetCenterEmpwise(Convert.ToInt32(Session["LoggedUserId"]))
                           .Select(x => x.Id).ToList();

//Get all employees having the above centercodids
List<int> _employeeIds = _db.EmployeeCenters
                        .Where(x => _centerCodeIds.Any(cc => x.Id == cc))
                        .Select(x => x.Employee.Id).ToList();

【问题讨论】:

  • @teovankot 我的错 ;)

标签: entity-framework linq


【解决方案1】:

这里是:

List<int> _employeeIds = _db.EmployeeCenters.Where(x => _db.EmployeeCenters
                        .Where(y => y.EmployeeId == 40)
                        .Select(y => y.CenterCodeId)
                        .Contains(x.CenterCodeId))
                      .Select(x => x.EmployeeId)
                      .ToList();

但是你确定你的查询中需要第二个子选择吗?

【讨论】:

    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多