如何结合查询这些数据

时间:2019-02-22 12:29:25

标签: c# entity-framework linq

我有两个桌子。

表A,存储软件包数据。 table a

PackageId | From  | To  | Created
-----------------------------------------
a1        | Japan | USA | 2019/2/22 16:00
a2        | Japan | USA | 2019/2/23 16:00
a3        | Japan | USA | 2019/2/24 16:00
a4        | Japan | USA | 2019/2/25 16:00
a5        | Japan | USA | 2019/2/26 16:00
a6        | Japan | USA | 2019/2/27 16:00
a7        | Japan | USA | 2019/2/28 16:00
a8        | Japan | USA | 2019/3/1  16:00
a9        | Japan | USA | 2019/3/2  16:00

表B,存储每个程序包的跟踪历史记录。将为运输中的每个包裹生成多个跟踪记录。 table b

PackageId | StaffId | Action   | UpdateDate
------------------------------------------------
a1        | s1      | Pick up  | 2019/2/23 15:00
a1        | s1      | transmit | 2019/2/24 15:00
a1        | s2      | transmit | 2019/2/25 15:00
a1        | s1      | transmit | 2019/2/26 15:00
a1        | s1      | transmit | 2019/2/27 15:00
a1        | s2      | transmit | 2019/2/28 15:00
a1        | s1      | transmit | 2019/3/1  15:00
a1        | s1      | transmit | 2019/3/2  15:00
a1        | s2      | Deliver  | 2019/3/3  15:00

我将创建一个页面,其中包含软件包列表以及每个软件包的最新状态。像这样: query result

Packages status page                

PackageId | From  | To  | Last Action | UpdateDate
-------------------------------------------------------
a1        | Japan | USA | Deliver     | 2019/3/3  15:00
a2        | Japan | USA | transmit    | 2019/3/4  15:00
a3        | Japan | USA | transmit    | 2019/3/5  15:00
a4        | Japan | USA | transmit    | 2019/3/6  15:00
a5        | Japan | USA | transmit    | 2019/3/7  15:00
a6        | Japan | USA | transmit    | 2019/3/8  15:00
a7        | Japan | USA | transmit    | 2019/3/9  15:00
a8        | Japan | USA | transmit    | 2019/3/10 15:00
a9        | Japan | USA | transmit    | 2019/3/11 15:00

我已经有一个SQL查询字符串:

SELECT A.*, B.*
FROM [Table A] AS A
JOIN
(
    SELECT TOP 1 WITH TIES *
    FROM [Table B]
    ORDER BY ROW_NUMBER() OVER (PARTITION BY PackageId ORDER BY UpdateDate DESC)
) AS B ON (b.PackageId = A.PackageId)

如何将上述Sql字符串转换为Linq?

如何通过EF / .Net核心进行查询?

2 个答案:

答案 0 :(得分:2)

给出此类以存储结果:

public class Result
{
    public string PackageId { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public DateTime UpdateDate { get; set; }
    public string LastAction { get; set; }
}

给定的aEntries包含AbEntries包含B
使用.Select()

List<Result> results = aEntries.AsEnumerable().Select(a =>
{
    var lastB = bEntries.OrderByDescending(b => b.UpdateDate)
                     .First(b => b.PackageId == a.PackageId);
    return new Result
    {
        PackageId = a.PackageId,
        From = a.From,
        To = a.To,
        LastAction = lastB.Action,
        UpdateDate = lastB.UpdateDate
    };
}).ToList();

或者,使用.Join()

List<Result> = aEntries.Join(bEntries, 
    a => a.PackageId, 
    b => b.PackageId,
    (a, b) => new Result()
    {
        PackageId = a.PackageId,
        To = a.To,
        From = a.From,
        LastAction = b.Action,
        UpdateDate = b.UpdateDate
    })
    .GroupBy(r => r.PackageId)
    .Select(g => g.OrderByDescending(r => r.UpdateDate).First()).ToList();

答案 1 :(得分:1)

您所需要做的就是这个:

Packages.Select(p => 
    new { 
       Package = p, 
       LastAction = p.Actions.OrderByDescending(a => a.UpdateDate).FirstOrDefault() 
        })

假设您的EF类是以这种方式设置的:

public class Package
{
    public int PackageId { get; set; } 
    public string From { get; set; } 
    public string To { get; set; } 
    public DateTime Created { get; set; } 

    public virtual ICollection<Action> Actions { get; set; } 
}

public class Action
{
    public int PackageId { get; set; } 
    public int StaffId { get; set; } 
    public string Action   { get; set; } 
    public DateTime UpdateDate { get; set; } 
}