Linq:嵌套查询比连接更好,但如果使用2个嵌套查询怎么办?

时间:2010-12-10 09:19:15

标签: linq entity-framework

在她的书中Entity Framework Julie Lerman建议使用嵌套查询而不是连接(向后滚动几页)。 在她的示例中,请参阅以这种方式填充1字段,但是您想要填充2的ID?

我在这里有一个示例,我希望使用相同的嵌套查询而不是2个单独的查询来填充Forename和Surname。我只需要知道正确的语法就可以了。

public static List<RequestInfo> GetRequests(int _employeeId)
{
    using (SHPContainerEntities db = new SHPContainerEntities())
    {
        return db.AnnualLeaveBookeds
            .Where(x => x.NextApproverId == _employeeId ||
            (x.ApproverId == _employeeId && x.ApprovalDate.HasValue == false)) 
            .Select(y => new RequestInfo
            {
                AnnualLeaveDate = y.AnnualLeaveDate,
                Forename = (
                    from e in db.Employees 
                    where e.EmployeeId == y.EmployeeId 
                    select e.Forename).FirstOrDefault(),
                Surname = (
                    from e in db.Employees 
                    where e.EmployeeId == y.EmployeeId 
                    select e.Surname).FirstOrDefault(),
                RequestDate = y.RequestDate,
                CancelRequestDate = y.CancelRequestDate,
                ApproveFlag = false,
                RejectFlag = false,
                Reason = string.Empty
            })
            .OrderBy(x => x.AnnualLeaveDate)
            .ToList();
    }
}

2 个答案:

答案 0 :(得分:3)

您的查询没有任何问题,但您可以使用更简单的方式编写它,而不使用嵌套查询:

public static List<RequestInfo> GetRequests(int employeeId)
{
    using (SHPContainerEntities db = new SHPContainerEntities())
    {
        return (
            from x in db.AnnualLeaveBookeds
            where x.NextApproverId == employeeId ||
                (x.ApproverId == employeeId && x.ApprovalDate == null)
            orderby x.AnnualLeaveDate
            select new RequestInfo
            {
                AnnualLeaveDate = x.AnnualLeaveDate,
                Forename = x.Employee.Forename,
                Surname = x.Employee.Surname,
                RequestDate = x.RequestDate,
                CancelRequestDate = x.CancelRequestDate,
                ApproveFlag = false,
                RejectFlag = false,
                Reason = string.Empty
            }).ToList();
    }
}

了解我刚刚删除了from e in db.Employees where ... select e.Forename)并将其替换为x.Employee.Forename。当您的数据库包含正确的外键关系时,EF设计器将成功生成在Employee实体上包含AnnualLeaveBooked属性的模型。像这样编写查询会使其更具可读性。

我希望这会有所帮助。

答案 1 :(得分:1)

试试这个

using (SHPContainerEntities db = new SHPContainerEntities())
{
    return db.AnnualLeaveBookeds
        .Where(x => x.NextApproverId == _employeeId ||
        (x.ApproverId == _employeeId && x.ApprovalDate.HasValue == false))
        .Select(y =>
            {
                var emp = db.Emplyees.Where(e => e.EmployeeId == y.EmployeeId);
                return new RequestInfo
                   {
                        AnnualLeaveDate = y.AnnualLeaveDate,
                        Forename = emp.Forename,
                        Surname = emp.Surname,
                        RequestDate = y.RequestDate,
                        CancelRequestDate = y.CancelRequestDate,
                        ApproveFlag = false,
                        RejectFlag = false,
                        Reason = string.Empty
                   };
            ).OrderBy(x => x.AnnualLeaveDate).ToList();
}