Hi @Anjali Agarwal,
You should move item.Files = new List<FileModel>()
outside the second loopand modify the sample code as follows:
using (var context = new SqlDataContext())
{
List<Employee> query = new List<Employee>();
query = _dbContext.Employee.Include(e => e.Files).ToList();
List<IndividualNominFilePath> individualNominFiles = _dbContext.IndividualNominFilePaths.ToList();
foreach (var employee in query)
{
// Initialize the Files property as a new list
employee.Files = new List<FilePathModel>();
foreach (var file in individualNominFiles)
{
if (file.EmployeeId == employee.EmployeeId)
{
// Add to the Files list instead of overwriting it
employee.Files.Add(new FilePathModel
{
EmployeeId = file.EmployeeId,
UploadFilePath = file.FilePath
});
}
}
}
And here is the test result.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Jason