我有我的數據庫中下表如何包括父對象子實體對象列表
Table1: tblAddressType (Id, Name)
Table2: tblAddressDtls (Id, AddressTypeId, Address1)
我左側的接合部上面兩個表來獲取所有的地址類型的列表和相應的詳細地址如下
SQL Query:
select t1.*, t2.*
from tblAddressType t1
left outer join tblAddressDtls t2 on t1.Id = t2.AddressTypeId and t2.Id = 1;
對於上述表中,我已創建POCO實體類如下:
[Table("tblAddressType ")]
public partial class AddressType
{
[Key]
[Column(Name="ID")]
public int ID { get; set; }
[Required]
[Column(Name = "Name")]
public virtual string Name {get; set;}
[Include]
[Association("AddressTypeAddress", "ID", "AddressTypeId")]
public virtual ICollection<Address> Addresses { get; set; }
}
[Table("tblAddress", SchemaName="dbo")]
public class Address
{
[Column(Name="ID")]
public int ID { get; set; }
[Column(Name = "AddressTypeId")]
public int? AddressTypeId{ get; set; }
[Column(Name = "Address1")]
public string Address1{ get; set; }
[Include]
[Association("AddressTypeAddress", "AddressTypeId", "ID", IsForeignKey = true)]
public virtual AddressType AddressType { get; set; }
}
,並以如上面的SQL查詢獲取數據,我在我的服務代碼寫了下面的LINQ查詢,該查詢返回我的數據,需要:
var qry = (from p in dbContext.AddressTypes
join pa in (from t in dbContext.Addresses
where t.ID == 1 select t)
on p.ID equals pa.AddressTypeId into ppa
from t in ppa.DefaultIfEmpty()
select t).AsQueryable();
現在,我想寫一域名服務方法「GetAddressById(int addressId)」,它應該返回匹配的Address對象以及AddressType對象列表,因爲我需要將「AddressType」對象列表綁定到Add/Edit地址屏幕中的下拉框。
I wanted to include and fetch list of "AddressType" objects data at the time of
fetching Address object data itself to avoid round-trip to server
in my silverlight client app.
請告訴我實現此功能的最佳方法嗎?
我已經更新了我的答案,如果這不是在做你想要的東西,那麼請糾正我,如果確實有幫助,那就接受它的答案。 – Joakim