简体   繁体   中英

Merge Fields in the Entity Framework

I'm using Entity Framework in ASP.NET and I have a class called Contacts and in it I have the following fields : Name , E- Mail and Phones. The field is a list Phones subclass type which has the following fields : PhoneNumber and Operator .

In Entity Framework can create , insert, change and delete normally in the database .

Now I need to export all contacts with your phone the first number from the list of phones to Json .

When I export the JSON looks like this .

[
  {
    " name " : " Name " ,
    "email" : " E- Mail"
    " phones" : [
      {
        " phonenumber " : " 000000000 "
        "operator " : "Operator "
      }
    ]
  }
]

I would like to join the fields of Contacts with Phones in the subclass Entity Framework so I can export as well :

[
  {
    " name " : " Name " ,
    "email" : " E- Mail"
    " phonenumber " : " 000000000 "
    "operator " : "Operator "
  }
]

In SQLServer I can do this:

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

How do the Entity Framework to merge the fields to export for Json as mentioned above ?

Use VB.NET but if not in vb can be in C # .

As you are using EntityFramework I'd do something else like this :

return Json(new
                        {
                            name = YourObject.Name,
                            email = YourObject.Email,
                            phonenumber = YourObject.Phones.PhoneNumber,
                            operator = YourObject.Operator 
                        });

Would that work for you ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM