【发布时间】:2022-01-21 06:17:05
【问题描述】:
我正在尝试使用以下对象、实体框架和 LINQ 从数据库中填充本地办公室联系人姓名列表的组合框。它不返回任何结果。有人可以帮忙吗? 这些类看起来像这样。
public class OfficeContact
{
[Key]
public int OfficeContactId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class Office
{
[Key]
public int OfficeId { get; set; }
public string Region { get; set; }
public string Address { get; set; }
public string City { get; set; }
[Display(Name = "OfficeContact")]
public virtual int OfficeContactId { get; set; }
[ForeignKey("OfficeContactId")]
public virtual OfficeContact OfficeContacts { get; set; }
}
LINQ 如下所示。
private void cbAddress_SelectionChangeCommitted(object sender, EventArgs e)
{
var localContact = from office in context.Offices.ToList()
where office.OfficeContacts.Equals(office.OfficeContactId)
select Name;
cbLocalOfficeContactName.DataSource = localContact.ToList();
}
【问题讨论】:
标签: c# entity-framework linq