将通用列表绑定到实体中的实体

本文关键字:实体 绑定 列表 | 更新日期: 2023-09-27 18:11:14

这是我使用linq将数据绑定到它的模型

public class Physician
 {
    public Physician()
    {
        PhysicianAddress = new Address();
    }      
    public int PhysicianId { get; set; } 
    public string NPI { get; set; }
    public string PhysicianPrefixName { get; set; }  
    public string PhysicianFirstName { get; set; }        
    public string PhysicianMiddleName { get; set; }        
    public string PhysicianLastName { get; set; }         
    public string PhysicianPhoneExt { get; set; }        
    public string PhysicianPhoneNum { get; set; }        
    public string PhysicianFaxNum { get; set; }        
    public string PhysicianEmailAddr { get; set; }        
    public string PhysicianComment { get; set; }        
    public List<Site> PracticeLocationsList { get; set; }        
    public Address PhysicianAddress { get; set; }        
    public int? MergedIntoPhysicianId { get; set; }
}

我从storedProcedure获取数据,然后我将返回结果绑定到模型

   var physicians = (from sp in e.GetSitePhysicians((int)request.SiteId)
                              select new Physician()
                              {
                                  PhysicianId = sp.PhysicianId,
                                  PhysicianFirstName = sp.PhysicianFirstName,
                                  PhysicianMiddleName = sp.PhysicianMiddleName,
                                  PhysicianLastName = sp.PhysicianLastName,
                                  NPI = sp.NPI,
                                  PhysicianPhoneNum = sp.PhysicianPhoneNum,
                                  PhysicianFaxNum = sp.PhysicianFaxNum,
                                  PhysicianEndDate = sp.PhysicianEndDate,
                                  PhysicianEffectiveDate = sp.PhysicianEffectiveDate,
                                  PhysicianComment = sp.PhysicianComment,
                                  AddressId = sp.AddressId,
                                  PhysicianEmailAddr = sp.PhysicianEmailAddr,
                                  PhysicianPhoneExt = sp.PhysicianPhoneExt,
                                  PhysicianAddress = new Address()
                                  {
                                      SiteAddressId =  sp.SiteAddressId ?? 0,
                                      Street1 =  sp.SiteAddressStreet1 ?? string.Empty,
                                      Street2 = sp.SiteAddressStreet2 ?? string.Empty,
                                      City = sp.SiteAddressCityName ?? string.Empty,
                                      PostalCode = sp.SiteAddressPostalCode ?? string.Empty,
                                      State =  sp.GeographicalStateProvinceCode ?? string.Empty,
                                      ParkingInstructions =  sp.SiteAddressParkingInstructions ?? string.Empty,
                                  },
                                  PracticeLocationsList=new Site()
                                            {
                                                Id = sp.SiteId,
                                                Name = sp.SiteName,
                                                SiteMainPhoneNum = sp.SiteMainPhoneNum,
                                                Address = new Address
                                                {
                                                    SiteAddressId = sp.SiteAddressId??0,
                                                    Street1 = sp.SiteAddressStreet1,
                                                    Street2 = sp.SiteAddressStreet2,
                                                    City = sp.SiteAddressCityName,
                                                    PostalCode = sp.SiteAddressPostalCode,
                                                    State = sp.GeographicalStateProvinceCode
                                                },
                                                Contacts = new    List<SiteContact>() { new SiteContact() }
                                            }
                              }).ToList();

当我试图将我的站点对象绑定到PracticeLocationsList时,它会生成编译时错误,"不能隐式地将类型'Site'转换为'System.Collections.Generic.List'"。

如何将我的站点对象转换为列表然后绑定?

将通用列表绑定到实体中的实体

因为您正在尝试将一个对象分配给列表。

试试这个:

PracticeLocationsList = new List<Site> { new Site()...

假设PracticeLocationsList可以分配给List<Site>,那么这个呢:

 PracticeLocationsList= new List<Site>()
                                     { new Site()
                                        {
                                            Id = sp.SiteId,
                                            Name = sp.SiteName,
                                            SiteMainPhoneNum = sp.SiteMainPhoneNum,
                                            Address = new Address
                                            {
                                                SiteAddressId = sp.SiteAddressId??0,
                                                Street1 = sp.SiteAddressStreet1,
                                                Street2 = sp.SiteAddressStreet2,
                                                City = sp.SiteAddressCityName,
                                                PostalCode = sp.SiteAddressPostalCode,
                                                State = sp.GeographicalStateProvinceCode
                                            },
                                            Contacts = new    List<SiteContact>() { new SiteContact() }
                                        }
                                     }