针对不同实例的SQL to Entity Framework查询

时间:2012-12-18 20:08:13

标签: sql entity-framework-4

我需要将以下查询从SQL转换为实体框架查询。任何人都可以帮忙。

                var query3 = string.Format(@"
                    SELECT DISTINCT
                        Boxes.BoxName,
                        Employees.EmployeeNumber,
                        Boxes.Port,
                        Boxes.SerialNumber
                    FROM
                        Eventlog

                        INNER JOIN Boxes 
                            ON Eventlog.SerialNumber = Boxes.SerialNumber

                        INNER JOIN Employees 
                            ON Eventlog.EmployeeId = Employees.EmployeeId

                    WHERE
                        ActionId = {4}
                        AND Boxes.Deactivated = 0
                        AND ( ('*' = '{0}')
                            OR (Boxes.SerialNumber = '{0}') )
                        AND ( (-1 = {1})
                            OR (Employees.EmployeeId = '{1}') )
                        AND EventTime >= '{2}'
                        AND EventTime <= '{3}'
                    ", boxFilter3, ReportEmployeeIdFilter, ReportStartDate, ReportEndDate, actionFilter);

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

var query = (from log in context.Eventlog
             join box in context.Boxes 
                on log.SerialNumber equals box.SerialNumber
             join employee in context.Employees 
                on log.EmployeeId equals employee.EmployeeId
             where log.ActionId == actionFilter 
             && box.Deactivated == 0
             && (boxFilter3 == "*" || box.SerialNumber == boxFilter3)
             && (ReportEmployeeIdFilter == -1 || employee.EmployeeId == ReportEmployeeIdFilter)
             && log.EventTime >= ReportStartDate
             && log.EventTime <= ReportEndDate
             select new
             {
                box.BoxName,
                employee.EmployeeNumber,
                box.Port,
                box.SerialNumber
             })
             .Distinct();