adding a list of items in another list

Anjali Agarwal 1,386 Reputation points
2023-10-03T03:18:57.4+00:00

I have the following code on my page:

This is the Model for Employee:

  public class Employee
  {
      public string Title { get; set; }
	  public string section { get; set; }
	  public string EmployeeName { get; set; }
	  public int employeeID { get; set; }
	  public List<FileModel> Files { get; set; }
  }

Below is the Model for IndividualNominFilePath

	public class IndividualNominFilePath
    {
	    public int IndividualId { get; set; }
		public int? EmployeeId { get; set; }
		public string FilePath { get; set; }
				
    }

In below code, I want to add FilePath in query List whenever the employeeId of IndividualNominFilePath is equal to Employee class EmployeeId


List<Employee> query = new List<Employee>();
            query = await _empContext.Set<Employee>().FromSqlRaw($"EXECUTE dbo.GetIndividual").ToListAsync();
            List<IndividualNominFilePath> individualNominFiles= _empContext.IndividualNominFilePaths.ToList();
                
                foreach (var item in query)
                {
                    foreach( var file in individualNominFiles)
                    {
                        if(file.EmployeeId == item.EmployeeID)
                        {
                            item.Files = new List<FileModel>()
                          {
                            new FileModel {EmployeeNumber=file.EmployeeId,FilePath=file.UploadedFilePath }
                          };
                            
                        }
                        
                    }
                    
              
                }

with above code, only the last files is added in the query list so the count of files is always 1. Please see below screen shot. How can I add all the files in the query List:

User's image

any help will be greatly appreciated.

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,811 Reputation points Microsoft Vendor
    2023-10-03T05:47:22.16+00:00

    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.User's image


    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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful