EF Core 2.1查询 - LEFT JOIN

时间:2018-06-01 00:37:01

标签: c# entity-framework-core ef-core-2.0

我正在尝试选择等效的

SELECT u.NodeId, 
       u.Name, 
       u.TierId, 
       u.OrgCode, 
       o.OrgName, 
       gtu.GroupId as ParentGroupId
FROM Unit u
LEFT JOIN GroupToUnit gtu ON u.NodeId = gtu.NodeId
JOIN Organisation o ON u.OrgCode = o.OrgCode

我的c#等价物是

IList<OrgChartNode> unitNodes = _db.Units
                                    .Where(u => u.OrgCode.Equals(OrgCode))
                                    .Select(u => new OrgChartNode
                                    {
                                         Id = u.NodeId,
                                         Name = u.Name,
                                         TierId = 0,
                                         ParentGroupId = u.GroupLinks.First().GroupId,
                                         OrgName = u.Organisation.Name,
                                         OrgCode = u.OrgCode,
                                         ContactName = null,
                                         ContactEmail = null,
                                         ContactPhone = null,
                                         ContactId = null,
                                    })
                                    .OrderBy(u => u.Name)
                                    .AsNoTracking()
                                    .ToList();

这是作为单位的初始查询翻译......

Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (5ms) [Parameters=[@__OrgCode_0='?' (Size = 5)], CommandType='Text', CommandTimeout='30']
SELECT [u].[NodeId] AS [Id0], [u].[ID] AS [Name0], [u.Organisation].[Name] AS [OrgName], [u].[OrgCode]
FROM [Report_Unit] AS [u]
INNER JOIN [Report_Organisation] AS [u.Organisation] ON [u].[OrgCode] = [u.Organisation].[OrgCode]
WHERE [u].[OrgCode] = @__OrgCode_0
ORDER BY [Name0]

然后每个Unit的子查询确定其GroupId。 (重复每条记录)

Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (1ms) [Parameters=[@_outer_NodeId='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [r1].[GroupId]
FROM [Report_Link_Group_to_Unit] AS [r1]
WHERE @_outer_NodeId = [r1].[NodeId]

如果单位没有联系,它也会失败。

如何修改查询以返回预期结果?我期望所有单位,如果不是链接,则在ParentGroupId中为null。另外,学习这种语法的阅读资源是什么?找到许多以前的版本示例并不是很有效。

1 个答案:

答案 0 :(得分:1)

我最终能够使用链接到EF类的数据库中的视图使此工作正常。我之前遇到的问题是由于我在最初的类定义中忽略了{get; set;}

public class OrgChartNode
{
    [Required]
    public int Id { get; set; }

    [MaxLength(100)]
    public string Name { get; set; }

    [Range(0, 6)]
    public int TierId { get; set; }

    public int? ParentGroupId { get; set; }

    [MaxLength(50)]
    public string OrgName { get; set; }

    [MaxLength(5)]
    public string OrgCode { get; set; }

    [MaxLength(100)]
    public string ContactName { get; set; }

    [MaxLength(255)]
    public string ContactEmail { get; set; }

    [MaxLength(12)]
    public string ContactPhone { get; set; }

    public int? ContactId { get; set; }

    public int? GroupTypeId { get; set; }

}

然后在DBContext中

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    ...
    builder
        .Query<OrgChartNode>()
        .ToView("vwAllOrgchartWithUnits");
}

最后,加载数据并转换为JSON

public JsonResult OnGet(string OrgCode)
{
    OrgChartNodes = _db.OrgChartNodes
                    .Where(g => g.OrgCode.Equals(OrgCode))
                    .ToList();

    return new JsonResult(OrgChartNodes);
}