简体   繁体   English

实体框架中的合并字段

[英]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. 我在ASP.NET中使用实体框架,并且有一个名为Contacts的类,其中有以下字段:Name,E-Mail和Phones。 The field is a list Phones subclass type which has the following fields : PhoneNumber and Operator . 该字段是列表Phones子类类型,其中包含以下字段:PhoneNumber和Operator。

In Entity Framework can create , insert, change and delete normally in the database . 在Entity Framework中可以正常在数据库中创建,插入,更改和删除。

Now I need to export all contacts with your phone the first number from the list of phones to Json . 现在,我需要将手机列表中的所有联系人的第一个号码导出到Json。

When I export the JSON looks like this . 当我导出JSON时,像这样。

[
  {
    " 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: 在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

How do the Entity Framework to merge the fields to export for Json as mentioned above ? 如上所述,实体框架如何合并要导出的字段以供Json使用?

Use VB.NET but if not in vb can be in C # . 使用VB.NET,但如果不在vb中,则可以在C#中使用。

As you are using EntityFramework I'd do something else like this : 当您使用EntityFramework时,我会做其他类似的事情:

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

Would that work for you ? 那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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