Если я всё правильно понял, то вот:
Исходные данные:
FirstReportInvoiceDal[] firstReportInvoice =
{
new () { Id = 1, Number = "" },
new () { Id = 2, Number = "" },
new () { Id = 3, Number = "" },
new () { Id = 4, Number = "" },
new () { Id = 5, Number = "" },
};
SecondReportInvoiceDal[] secondReportInvoice =
{
new () { FirstReportId = 1, Number = "1111N" },
new () { FirstReportId = 2, Number = "2222N" },
new () { FirstReportId = 3, Number = "3333N" },
new () { FirstReportId = 4, Number = "4444N" },
new () { FirstReportId = 5, Number = "5555N" },
};
Вывод:
Id: 1, Number: 1111N
Id: 2, Number: 2222N
Id: 3, Number: 3333N
Id: 4, Number: 4444N
Id: 5, Number: 5555N
namespace ConsoleApp;
class Program
{
public static void Main(string[] args)
{
FirstReportInvoiceDal[] firstReportInvoice =
{
new () { Id = 1, Number = "" },
new () { Id = 2, Number = "" },
new () { Id = 3, Number = "" },
new () { Id = 4, Number = "" },
new () { Id = 5, Number = "" },
};
SecondReportInvoiceDal[] secondReportInvoice =
{
new () { FirstReportId = 1, Number = "1111N" },
new () { FirstReportId = 2, Number = "2222N" },
new () { FirstReportId = 3, Number = "3333N" },
new () { FirstReportId = 4, Number = "4444N" },
new () { FirstReportId = 5, Number = "5555N" },
};
ReportRow[] rows = CreateReportRows(firstReportInvoice, secondReportInvoice);
Display(rows);
}
private static void Display(IEnumerable<ReportRow> rows)
{
foreach (ReportRow row in rows)
{
Console.WriteLine(row);
}
}
static ReportRow[] CreateReportRows(FirstReportInvoiceDal[] firstReportInvoice,
SecondReportInvoiceDal[] secondReportInvoice)
{
// Для быстрого поиска делаем словарь с ключом SecondReportInvoiceDal.FirstReportId,
// который соответствует ключу FirstReportInvoiceDal.Id.
var secondReportSet = secondReportInvoice.ToDictionary(x => x.FirstReportId);
// Для каждого firstReportInvoice сделать трансформацию (LINQ метод Select)
return firstReportInvoice.Select(x =>
{
var row = new ReportRow
{
Id = x.Id,
Number = secondReportSet[x.Id].Number
};
return row;
// Вызываем ToArray, чтобы запустилось выполнение трансформации в методе Select.
// И чтобы в принципе мы вернули данные, а не итератор.
}).ToArray();
}
}
class FirstReportInvoiceDal
{
public int Id { get; init; }
public string Number { get; init; }
}
class SecondReportInvoiceDal
{
public int FirstReportId { get; init; }
public string Number { get; init; }
}
class ReportRow
{
public int Id { get; init; }
public string Number { get; init; }
public override string ToString()
{
return $"Id: {Id}, Number: {Number}";
}
}