使用MAX的实体框架查询

时间:2011-12-21 17:40:25

标签: c# entity-framework entity-framework-4 linq-to-entities

我一直在试图将一个简单的SQL查询翻译成EF查询。

任何人都可以帮忙。以下是我试图翻译的查询。

SELECT p.[UniqueId]
  ,p.[CAI]
  ,p.[HRGuid]
  ,p.[FullName]
  ,p.[Email]
  ,a.*
FROM [Participant] p
INNER JOIN 
(
    Select * FROM Assignment where assignmentNumber = 
    (Select MAX(AssignmentNumber)FROM 
    Assignment GROUP BY UniqueId)
) a
ON p.UniqueId = a.UniqueId

基本上我正试图让参与者和他们的最新作业一起。

1 个答案:

答案 0 :(得分:0)

您需要使用Linq-Objects创建Participant实体。您需要在AsEnumerable之后进行自定义才能创建实体

var query = (from p in context.Participant
            join a in context.Assignment on p.UniqueId equals a.UniqueId into ag
            select new
            {
              Participant = p,
              Assignment = ag.OrderByDescending(x => x.AssignmentNumber).FirstOrDefault()
            }).AsEnumerable()
              .Select(x => new Participant(x.Participant )
                     {    
                        Assignments = new Assignment[] { x.Assignment }
                     };