EF Core Linq查询语法

时间:2018-05-25 00:36:51

标签: linq entity-framework-core ef-core-2.0

我正在尝试在EF中复制一个视图。原始视图使用了UNION运算符,因此在此处将我的初始查询拆分为更好的匹配。有一些问题解决了我的语法问题。我正在尝试查询群组,获取他们的主要联系人详细信息。同时获取所有车辆及其链接组。我相信我已在第一个查询中正确指定了ContactLink要求。

[BindProperty]
public IList<OrgChartNode> OrgChartNodes { get; set; }

public JsonResult OnGet(string OrgCode)
{
            IList<OrgChartNode> OrgChartGroups = _db.Groups
                                                .Include(g => g.ContactLinks)
                                                    .ThenInclude(gtc => gtc.Contact)
                                                .Include(g => g.Organisation)
                                                .Where(g => g.OrgCode.Equals(OrgCode))
                                                .Where(g => g.ContactLinks.Any(cl => cl.LinkTypeId == 1))
                                                .Select(g => new OrgChartNode
                                                {
                                                    Id = g.Id,
                                                    Name = g.Name,
                                                    TierId = g.TierId,
                                                    ParentGroupId = g.ParentGroupId,
                                                    OrgName = g.Organisation.Name,
                                                    OrgCode = g.OrgCode,
                                                    ContactName = g.ContactLinks.**Contact**.Name,
                                                    ContactEmail = g.ContactLinks.**Contact**.Phone,
                                                    ContactPhone = g.ContactLinks.**Contact**.Email,
                                                    ContactId = g.ContactLinks.**Contact**.ContactId,
                                                })
                                                .OrderByDescending(g => g.TierId)
                                                .ThenBy(g => g.Name)
                                                .AsNoTracking()
                                                .ToList();

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

        OrgChartNodes = OrgChartGroups.Concat(OrgChartUnits)
                                .ToList();

        return new JsonResult(OrgChartNodes);
}

四个联系人字段已标记为**。它们在Visual Studio中有红色intellisense线。看起来像cat跟随联系,尽管它是ThenInclude()以上。第二个查询中的GroupId也是如此。将鼠标悬停在首字母缩略词/别名上会告诉我它们似乎是正确的类。

Table Diagram

下面列出了三个数据类。您可以看到导航属性。它们配置正确吗?

如何在.Select()来电中添加联系方式?

Group.cs

[Table("Report_Group")]
public class Group
{
    [Key, Required, Column("GroupId")]
    public int Id { get; set; }

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

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

    public int TierId { get; set; }

    public int? ParentGroupId { get; set; }

    public int? CostCenter { get; set; }

    [Required]
    public int ExcludeFromAlertStats { get; set; }

    [Required]
    public int GroupTypeId { get; set; }

    [ForeignKey("ParentGroupId")]
    public virtual Group Parent { get; set; }

    [ForeignKey("OrgCode")]
    public virtual Organisation Organisation { get; set; }

    [ForeignKey("TierId")]
    public virtual Tier Tier { get; set; }

    [ForeignKey("GroupTypeId")]
    public virtual GroupType GroupType { get; set; }

    [InverseProperty("GroupId")]
    public virtual IList<GroupToUnitLink> UnitLinks { get; set; }

    [InverseProperty("GroupId")]
    public virtual IList<GroupToContactLink> ContactLinks { get; set; }
}

GroupToContactLink.cs

此类键是使用流畅的OnModelCreating()设置的,因为它是一个复合键。

[Table("Report_Link_Group_to_Contact")]
public class GroupToContactLink
{
    public GroupToContactLink()
    {
    }

    public GroupToContactLink(int contactId, int groupId, int linkTypeId)
    {
        this.ContactId = contactId;
        this.GroupId = groupId;
        this.LinkTypeId = linkTypeId;
    }

    [Required]
    public int ContactId { get; set; }

    [Required]
    public int GroupId { get; set; }

    [Required]
    public int LinkTypeId { get; set; }

    [ForeignKey("ContactId")]
    public virtual Contact Contact { get; set; }

    [ForeignKey("GroupId")]
    public virtual Group Group { get; set; }

    [ForeignKey("LinkTypeId")]
    public virtual ContactLinkType LinkType { get; set; }

}

Contact.cs

[Table("Report_Contact")]
public class Contact
{
    /// <summary>
    /// Primary Key for Contact in the database
    /// </summary>
    [Key, Column("ContactId")]
    public int Id { get; set; }

    /// <summary>
    /// Foreign Key indicating <see cref="Data.Organisation"/>
    /// </summary>
    [Required, StringLength(5)]
    public string OrgCode { get; set; }

    /// <summary>
    /// Name of Contact
    /// </summary>
    [Required, StringLength(100)]
    public string Name { get; set; }

    /// <summary>
    /// Phone number of contact.  Needs to be in +614 format for SMS to work
    /// </summary>
    [StringLength(12)]
    public string Phone { get; set; }

    /// <summary>
    /// Email Address for Contact
    /// </summary>
    [StringLength(255), EmailAddress]
    public string Email { get; set; }

    /// <summary>
    /// Navigation property to <see cref="Data.Organisation"/>
    /// </summary>
    [ForeignKey("OrgCode")]
    public virtual Organisation Organisation { get; set; }

    [InverseProperty("ContactId")]
    public virtual IList<GroupToContactLink> GroupLinks { get; set; }
}

0 个答案:

没有答案