What is the different between List into Raw View and List into Array ?

Jerry Lipan 916 Reputation points
2021-07-08T23:37:54.657+00:00

I've result set as following,

Raw View
113019-09072021-001.png

Into Array
113020-09072021-002.png

How to convert List Raw View into List of Array?
113131-09072021-001.png

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

Accepted answer
  1. Jaliya Udagedara 2,836 Reputation points MVP Volunteer Moderator
    2021-07-09T02:18:59.523+00:00

    Right, so looking at the code, _employeeDM.GetAll() returns an EmployeeVM which cannot be converted to an array. However I see that it contains a List<EmploeeVM.Employee>, and that can be converted to an array.

    For example,

    EmployeeVM viewModel = _employeeDM.GetAll();
    var employeesArray = viewModel.Employees.ToArray()
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jerry Lipan 916 Reputation points
    2021-07-09T00:51:56.387+00:00

    Hi @Jaliya Udagedara ,

    For return type, see the code

     public class EmployeeController : Controller  
        {  
            public static List<EmployeeVM> emp = new List<EmployeeVM>();  
      
            private IDepartmentDM _departmentDM;  
      
      
            private IEmployeeDM _employeeDM;  
    
    public EmployeeController(IDepartmentDM departmentDM, IEmployeeDM employeeDM)  
            {  
                _departmentDM = departmentDM;  
                _employeeDM = employeeDM;  
            }  
      
              
            public IActionResult Index()  
            {              
                ViewBag.DataSource = _employeeDM.GetAll();  
                 
                return View();  
            }  
      
    
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Threading.Tasks;  
      
    namespace TAFACoreMvc.Models  
    {  
        public interface IEmployeeDM  
        {  
            EmployeeVM GetAll();  
        }  
    }  
    

    This is the method

     public EmployeeVM GetAll()  
            {  
                var vm = new EmployeeVM();  
      
                var dtos = svc.GetAll().ToList();  
      
                vm.Employees.AddRange(dtos.Select(dto => new EmployeeVM.Employee()  
                {  
                    EmployeeId = dto.EmployeeId,  
                    DepartmentId = dto.DepartmentId,  
                    DepartmentName = dto.DepartmentName,  
                    EmployeeName = dto.EmployeeName,  
                    Designation = dto.Designation  
                }).ToList());  
      
                return vm;  
            }  
    

    113010-09072021-003.png

    The return type is List<T>

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.