Home > database >  Fill list null values with another list values
Fill list null values with another list values

Time:08-12

I have the two following lists of different objects that are being filled with different values:

page.Invoice.InvoiceDet = page.UcDetail.InvoiceItems;
page.Invoice.InvoiceTax = page.UcTax.InvoiceItems;

In InvoiceDet i have a property called TaxCodeDesc that brings the description of a Tax Code, in InvoiceTax i have the same property but is null in all the items. Both lists have the same TaxCode so i want to use this value to compare and fill InvoiceTax with TaxCodeDesc

This is what i've tried.

page.UcDetail.InvoiceItems.ForEach(x => x.TaxCodeDesc = page.UcTax.InvoiceItems[0].TaxCodeDesc);

I appreciate any responses. Thanks

CodePudding user response:

You need to find target object by finding with common property eg. Id

page.UcDetail
  .InvoiceItems
  .ForEach(x => x.TaxCodeDesc = page.UcTax.InvoiceItems.FirstOfDefault(y=> y.Id == x.Id)?.TaxCodeDesc);

CodePudding user response:

You must first find any item with TaxCode and then set TaxCodeDesc.

foreach (var item in page.Invoice.InvoiceTax)
{
    item.TaxCodeDesc = page.Invoice.InvoiceDet.FirstorDefault(x => x.TaxCode = item.TaxCode)?.TaxCodeDesc;
}
  • Related