我正在嘗試使用以下物件、物體框架和 LINQ 從資料庫中使用本地辦公室聯系人的電子郵件地址填充文本框。這是我正在使用的 LINQ:
var localContactsEmail = from office in context.Offices
where office.Address.ToString().Equals(selectedAddress)
select office.OfficeContacts.Email;
tbLocalOfficeEmail.Text = localContactsEmail.ToString();
如果我在 localContactsEmail 上使用 ToString() 文本框顯示:
SELECT
[Extent2].[Email] AS [Email] FROM [dbo].[Offices] AS [Extent1]
INNER JOIN [dbo].[OfficeContacts] AS [Extent2] ON [Extent1].[OfficeContactId] = [Extent2].[OfficeContactId]
WHERE ((CASE WHEN ([Extent1].[Address] IS NULL) THEN N'' ELSE [Extent1].[Address] END) = @p__linq__0) OR ((CASE WHEN ([Extent1].[Address] IS NULL) THEN N'' ELSE [Extent1].[Address] END IS NULL) AND (@p__linq__0 IS NULL))
如果我在 localContactsEmail 上離開 ToString() 我會收到以下錯誤:
錯誤 CS0029 無法將型別“System.Linq.IQueryable”隱式轉換為“字串”
誰能告訴我為什么它不只回傳電子郵件地址。
這里是物件。
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; }
}
uj5u.com熱心網友回復:
localContactsEmail 是可查詢的。你能試試這個代碼嗎:
var localContactsEmail = (from office in context.Offices
where office.Address.ToString().Equals(selectedAddress)
select office.OfficeContacts.Email).FirstOrDefault();
uj5u.com熱心網友回復:
替換tbLocalOfficeEmail.Text = localContactsEmail.ToString();為
if (localContactsEmail.Any())
{
tbLocalOfficeEmail.Text = localContactsEmail.First().ToString();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402047.html
