[英]Merge Fields in the Entity Framework
我在ASP.NET中使用實體框架,並且有一個名為Contacts的類,其中有以下字段:Name,E-Mail和Phones。 該字段是列表Phones子類類型,其中包含以下字段:PhoneNumber和Operator。
在Entity Framework中可以正常在數據庫中創建,插入,更改和刪除。
現在,我需要將手機列表中的所有聯系人的第一個號碼導出到Json。
當我導出JSON時,像這樣。
[
{
" name " : " Name " ,
"email" : " E- Mail"
" phones" : [
{
" phonenumber " : " 000000000 "
"operator " : "Operator "
}
]
}
]
我想在子類實體框架中加入“電話聯系”字段,以便我也可以導出:
[
{
" name " : " Name " ,
"email" : " E- Mail"
" phonenumber " : " 000000000 "
"operator " : "Operator "
}
]
在SQLServer中,我可以這樣做:
select
name
, email
, ( select top 1 phonenumber from phones where contact_id = contact.id )
, ( select top 1 operator from phones where contact_id = contact.id )
from contact
如上所述,實體框架如何合並要導出的字段以供Json使用?
使用VB.NET,但如果不在vb中,則可以在C#中使用。
當您使用EntityFramework時,我會做其他類似的事情:
return Json(new
{
name = YourObject.Name,
email = YourObject.Email,
phonenumber = YourObject.Phones.PhoneNumber,
operator = YourObject.Operator
});
那對你有用嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.