1

我正在使用 MVC3 并且仍在学习 LINQ。我在尝试将查询转换为 LINQ to Entities 时遇到了一些麻烦。我想返回一个员工对象。

SELECT E.EmployeeID, E.FirstName, E.LastName, MAX(EO.EmployeeOperationDate) AS "Last Operation"
FROM Employees E 
INNER JOIN EmployeeStatus ES ON E.EmployeeID = ES.EmployeeID
INNER JOIN EmployeeOperations EO ON ES.EmployeeStatusID = EO.EmployeeStatusID
INNER JOIN Teams T ON T.TeamID = ES.TeamID
WHERE T.TeamName = 'MyTeam'
GROUP BY E.EmployeeID, E.FirstName, E.LastName
ORDER BY E.FirstName, E.LastName

我有几张表,但我只需要根据 EmployeeOperationDate 获取最新状态。这似乎在 SQL 中运行良好。我也在使用 Ninject 并将我的查询设置为返回 Ienumerable。我玩了按选项分组,但它随后返回 IGroupable。任何有关转换和返回属性对象类型的指导将不胜感激。

编辑:我开始在 LINQ 中写出来,但我不确定如何正确返回正确的类型或强制转换它。

public IQueryable<Employee> GetEmployeesByTeam(int teamID)
    {

        var q = from E in context.Employees
                join ES in context.EmployeeStatuses on E.EmployeeID equals ES.EmployeeID
                join EO in context.EmployeeOperations on ES.EmployeeStatusID equals EO.EmployeeStatusID
                join T in context.Teams on ES.TeamID equals T.TeamID
                where T.TeamName == "MyTeam"
                group E by E.EmployeeID into G
                select G;
        return q;
    }

编辑2:

这似乎对我有用

public IQueryable<Employee> GetEmployeesByTeam(int teamID)
    {

        var q = from E in context.Employees
                join ES in context.EmployeeStatuses on E.EmployeeID equals ES.EmployeeID
                join EO in context.EmployeeOperations.OrderByDescending(eo => eo.EmployeeOperationDate) on ES.EmployeeStatusID equals EO.EmployeeStatusID
                join T in context.Teams on ES.TeamID equals T.TeamID
                where T.TeamID == teamID
                group E by E.EmployeeID into G
                select G.FirstOrDefault();
        return q;
    }
4

2 回答 2

1

杰夫,您的“Edit2”也为我提供了帮助和工作!我是这样做的:

public IEnumerable<ListCases> GetListCases(int? customerId, int? languageId, int? userGroupId) 
{    
    var query = from cfs in this.DataContext.CaseFieldSettings
            join cfsl in this.DataContext.CaseFieldSettingLanguages on cfs.Id equals cfsl.CaseFieldSettings_Id  
            join cs in this.DataContext.CaseSettings on cfs.Name equals cs.Name
            where cfsl.Language_Id == languageId && cfs.ShowOnStartPage == 1 && cfs.Customer_Id == customerId && cs.UserGroup == userGroupId && cs.Name != null  
            group cfs by new  {  
                                    cfs.Id, cfs.Customer_Id, cfsl.CaseFieldSettings_Id, cfsl.Label, cfsl.Language_Id, cfs.ShowOnStartPage, cs.Name  
                              } into grouped  

            select new ListCases  
            {  
                Id = grouped.Key.Id, CFS_Id = grouped.Key.CaseFieldSettings_Id, Label = grouped.Key.Label, Language_Id = grouped.Key.Language_Id, Customer_Id = grouped.Key.Customer_Id, Show = grouped.Key.ShowOnStartPage, CSName = grouped.Key.Name  
            };

    return query;
}
于 2012-10-24T10:58:46.970 回答
0

我不太清楚你的 SQL 查询应该如何工作,但我试图塑造你的 LINQ 查询以匹配它正在做的事情并且它也返回 type Employee。这应该可以帮助您获得所需的确切解决方案:

var q = from E in context.Employees
         join ES in context.EmployeeStatuses on E.EmployeeID equals ES.EmployeeID
         join EO in context.EmployeeOperations
             .OrderByDescending (x => x.EmployeeOperationDate) 
                 on ES.EmployeeStatusID equals EO.EmployeeStatusID
         join T in context.Teams on ES.TeamID equals T.TeamID
         where T.TeamName == "MyTeam"
         group new { E.EmployeeID, E.FirstName, E.LastName, EO.EmployeeOperationDate } 
             by new {E.EmployeeID, E.FirstName, E.LastName } into G
         orderby G.Key.LastName,G.Key.FirstName
         select new Employee
         {
             EmployeeID = G.Key.EmployeeID,
             FirstName = G.Key.FirstName,
             LastName = G.Key.LastName,
             LastOperation = G.First ().EmployeeOperationDate
         };
于 2012-07-22T17:25:32.410 回答