0

我需要将以下查询从 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);
4

1 回答 1

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();
于 2012-12-18T20:36:00.220 回答