Search code examples
c#entity-frameworkef-code-first

Call a method that return a list of employee from Class Library to UI DataGridView project C# Entity Framework


Im new in Entity Framework and LINQ I have two project one is my class library the other one is my UI project I'm struggling to show a list of employee from method that return the list from my class library This is my class:

    public static class ViewEmployeesDataManager
    {
        public static List<Employee> ViewManagerEmployees()
        {
            using (var context = new HRSystemContext())
            {
                var query = from empolyee in context.Employees
                            select new { 
                                Name = empolyee.FullName,
                                JobTitle = empolyee.JobTitle,
                                Mobile = empolyee.Mobile};

                return query.ToList();

this is how I call it in my form:

dataGridView1.DataSource=ViewEmployeesDataManager.ViewManagerEmployees();

I got this error

error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string Name, string JobTitle, string Mobile>>' to 'System.Collections.Generic.List<DataAccessLayer.Employee>

anyone can help please


Solution

  • UPDATE: I solved the error by adding Object instead of Employee in my list and it's worked

    public static List<Object> ShowingEmployeeInfo()
        {
            using (var context = new HRSystemContext())
            {
                var employee = from row in context.Employees
                                where row.Username == EmployeeDataManager.currentUser
                                select new {
                                    Id = row.Id,
                                    Name = row.FullName,
                                    Job = row.JobTitle,
                                    Mobile = row.Mobile,
                                    Manager = row.Manager.FullName
                                };
                return employee.ToList<Object>();
    
            }
        }