将LINQ转换为SQl存储过程

时间:2018-05-28 10:14:44

标签: sql linq

我正在尝试将此linq查询转换为sql存储过程。

var result =
                    from employee in employees.
                        Where
                        (
                            f => bool.Parse(f.Field<string>("isActive"))
                        )

                    from department in departments.
                Where
                (
                    d =>
                        int.Parse(d.Field<string>("empid")) ==
                            int.Parse(employee.Field<string>("empid"))
                ).
                DefaultIfEmpty()

                    from student in students.
                    Where
                    (
                        b =>
                            int.Parse(b.Field<string>("empid")) ==
                                int.Parse((string)employee.Field<string>("empid"))
                    ).
                    DefaultIfEmpty()

                    group new { employee = employee, department = department, student = student } by
                    new
                    {

                        emp_name = employee.Field<string>("empname"),
                        has_benefits =
                            bool.Parse
                            (

                                department == null ?
                                employee.Field<string>("benefits") :
                                department.Field<string>("benefits")
                            ),

                        dept_name =
                            department == null ?
                            string.Empty :
                            department.Field<string>("deptname") as string,
                    }
                        into grp
                    select
                        new
                        {
                            name = grp.Key.emp_name,
                            has_benefits = grp.Key.has_benefits,
                            dept_name = grp.Key.dept_name,
                            local =
                                grp.Any
                                (
                                    entry =>
                                        entry.student != null &&
                                        entry.student.Field<string>("code").Equals("local", StringComparison.OrdinalIgnoreCase)
                                ),

                        };

我不确定select语句应该如何。我试过这个

SELECT * FROM employee e
JOIN department d ON e.empid = d.empid 
JOIN student s ON s.empid = e.empid
WHERE e.isActive = true
GROUP BY 
emp_name, 
CASE( WHEN d == null THEN e.benefits ELSE d.benefits) as has_benefits 
CASE( WHEN d == null THEN '' ELSE d.deptname) as dept_name

我不确定应该如何转换群组。 我在proc中尝试了这个但是它没有给我相同的结果。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

如果您共享表格布局或设计会更好。尝试以下查询,但我没有测试它。

    SELECT 
        empname,
        deptname,
        CASE WHEN deptname IS NULL THEN E.benefits ELSE D.benefits END AS has_benefits,
        CASE WHEN S.code = S.local THEN 1 ELSE 0 END AS [local]
    FROM employees AS E
    LEFT JOIN departments AS D ON E.empid = D.empid
    LEFT JOIN students AS S ON E.empid = E.empid
    WHERE E.isActive = 1
    GROUP BY
    empname,
    deptname,
    has_benefits,
    [local]