嵌套Linq转换(选择)

时间:2017-04-12 10:27:09

标签: c# linq

我试图将对象(Lead)转换为同一对象的缩小形式(ApiLead) 该对象包含一个对象列表(OtherInHousehold),它也需要减少(ApiOtherInHousehold):

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 个答案:

答案 0 :(得分:0)

在linq声明中,您将返回List<ApiLead>。但结果变量是List<Lead>。您必须将结果变量声明为List<ApiLead>

希望有所帮助,