LINQ / C#:在每个组中加入,分组并选择“最后”吗?

时间:2019-02-25 22:16:51

标签: c# linq

使用C#和LINQ,我有一个员工表,其中只有一些处于活动状态,还有一个事件表。我需要将事件表加入ID上的员工,按员工对事件进行分组,然后选择最新事件。我已经尝试过类似的方法,但是却变得目不暇接。

 var emp = (from em in dc.Employees
                   where (em.Active == true)
                   join ev in dc.Events on em.ID equals ev.Employee orderby ev.Time

好的,通过蚕食另一个项目中的一些旧代码,我得到了:

 var attends = dc.Events               
 .GroupBy(c => c.Employee)
 .Select(x => x.OrderByDescending(y => y.Time).FirstOrDefault())
 .Join(dc.Employees,u => u.Employee,o => o.ID,
        (u, o) => new { o.Name,u.Time,u.EventType}).ToList();

这似乎可行,但现在我需要过滤“ active”为true的“ Employees ...我该在哪里做?”

好的...我想(敲木头)我明白了,如果它可以帮助别人,这似乎可以做到:

var attends = dc.Events                
 .GroupBy(c => c.Employee)
 .Select(x => x.OrderByDescending(y => y.Time).FirstOrDefault())
 .Join(dc.Employees.Where (z=>z.Active), u => u.Employee,o => o.ID, 
        (u, o) => new { o.Name,u.Time,u.EventType}).ToList();


    }

如果没有,我会哭泣的。。。

3 个答案:

答案 0 :(得分:0)

类似这样的东西(可能无法编译):

var emp = (from ii in
        (from em in dc.Employees
               where (em.Active == true)
               join ev in dc.Events on em.ID equals ev.Employee 
        select new { Employee = ev.Employee, Event = ev.Eventid, EventTime = ev.Time})
        group ii by new {ii.EventId} into g
        select new 
        {
           Event = g.Key
        }).ToList();

答案 1 :(得分:0)

看起来像这样的作品...让我感到沮丧的是我投了反对票,因为显然有人觉得我没有做足够的功课。希望这会帮助其他人:

    var attends = dc.Events                
 .GroupBy(c => c.Employee)
 .Select(x => x.OrderByDescending(y => y.Time).FirstOrDefault())
 .Join(dc.Employees.Where (z=>z.Active), u => u.Employee,o => o.ID, 
        (u, o) => new { o.Name,u.Time,u.EventType}).ToList();

答案 2 :(得分:0)

  

我需要将事件表加入ID上的员工,按员工将事件分组并选择最新事件。

在我看来,您想要一系列雇员,每个雇员都有他的最新事件。

通常,如果先加入Join,然后再加入GroupBy,则还可以使用GroupJoin:

var employeesWithNewestEvents = employees.GroupJoin(events,
    employee => employee.Id,        // from each Employee take the primary key
    event => event.EmployeeId,      // from each event take the foreign key to Employee

    // ResultSelector: from each Employee with his zero or more Events, make a new
    (employee, eventsOfThisEmployee) => new
    {
         // select only the Employee properties you plan to use
        Id = employee.Id,
        Name = employee.Name,
        ...

        NewestEvent = eventsOfThisEmployee
            .OrderByDescending(event => event.Time)
            .Select(event => new
            {
                 // Select only the Event Properties you plan to use
                 Id = event.Id,
                 Description = event.Description,
                 ...

                 // not needed, you know the value: EmployeeId = event.EmployeeId
            })
            .FirstOrDefault(),
    })

请注意,没有事件的员工也将出现在所选数据中。它们的NewestEvent值为空。

如果要选择完整的Employee及其完整的最新事件,可以使用以下结果选择器:

(employee, eventsOfThisEmployee) => new
{
     Employee = employee,
     NewestEvent = eventsOfThisEmployee
        .OrderByDescending(event => event.Time)
        .FirstOrDefault(),
});

请注意,此方法将选择比实际需要更多的属性。