2017-04-12 151 views
0

我試圖將對象(主角)轉換爲同一對象的簡化形式(ApiLead) 該對象包含還需要縮小的對象列表(OtherInHousehold)(ApiOtherInHousehold):嵌套Linq轉換(選擇)

result = leads.Select(lead => new ApiLead() 
{ 
    UserId = lead.UserId, 
    DepartmentId = lead.DepartmentId, 
    CompanyId = lead.CompanyId, 
    CPR_number = lead.CPR_number, 
    CVR_number = lead.CVR_number, 
    Name = (lead.FirstName == "[virksomhed]" ? "" : lead.FirstName + " ") + lead.LastName, 
    Address = (lead.Street + " " + lead.StreetNumber + " " + lead.Floor + " " + lead.Side).Trim(), 
    Zipcode = lead.Zipcode, 
    City = (lead.PlaceName + " " + lead.City).Trim(), 
    Phonenumber = ("Fastnet: " + lead.Phonenumber + " Mobil: " + lead.Cellphonenumber), 
    Email = lead.Email, 
    BestReached = lead.BestReached, 
    **OthersInHousehold = lead.OthersInHousehold.Select(oih => new ApiOtherInHousehold(){ CPR_number = oih.CPR_number, Name = oih.Name }).ToList()**, 
    WantsVisit = lead.WantsVisit, 
    WantsPhonecall = lead.WantsPhonecall, 
    WantsInsuranceImmediately = lead.WantsInsuranceImmediately, 
    ExistingInsurance = lead.ExistingInsurance, 
    CurrentInsuranceCompany = lead.CurrentInsuranceCompany, 
    OtherInfo = lead.OtherInfo, 
    Status = lead.Status, 
    CreationDate = lead.CreationDate 
}).ToList(); 

但是,這將引發

Cannot implicitly convert type 'System.Collections.Generic.List<LeadsApp.ApiModels.ApiLead>' to 
'System.Collections.Generic.List<LeadsApp.Models.Lead>' 

這是不可能的還是我做錯了?

謝謝,夥計們。

+1

什麼是結果變量的聲明類型? – Berkay

+0

它的列表結果 –

+0

你將不得不編寫代碼將ApiLead轉換爲Lead。它不會神奇地發生。 –

回答

0

在linq聲明中,您將返回List<ApiLead>。但結果變量是List<Lead>。你必須聲明你的結果變量爲List<ApiLead>

Hope help,