Home > Software design >  Linq wont return List
Linq wont return List

Time:11-19

        public IEnumerable<ProfileVM> GetEmployees()
        {
            var register2 = from a in context.Employees
                            join b in context.Accounts on a.NIK equals b.NIK
                            select new
                            {
                                a.NIK,
                                b.Password,
                            };
            return register2.ToList();
        }

I have a Repository to call data using LingQ, I want to return it so I can pass it to my controller, but I can't return it

return (IEnumerable<ProfileVM>)register2.ToList();

The IDE suggest this but it still doesn't work the error said : System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType83[System.String,System.String,System.Int32]]' to type 'System.Collections.Generic.IEnumerable`1[API.ViewModel.ProfileVM]'.'

MyProfileVM

public class ProfileVM
    {
        public string NIK { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string Phone { get; set; }
        public string Password { get; set; }
        public DateTime BirthDate { get; set; }
        public int Salary { get; set; }
        public string Email { get; set; }
        public int GPA { get; set; }
        public string Degree { get; set; }
        public int UniversityId { get; set; }
    }

CodePudding user response:

Make sure that the properties is mapping your view model. Then try add your class*(Your case is ProfileVM)* to the linq select new.

        public IEnumerable<ProfileVM> GetEmployees()
        {
            var register2 = from a in context.Employees
                            join b in context.Accounts on a.NIK equals b.NIK
                            join c in context.Profilings on b.NIK equals c.NIK
                            select new ProfileVM
                            {
                                a.NIK,
                                b.Password,
                                c.EducationId
                            };
            return register2.ToList();
        }

CodePudding user response:

Since GetEmployees() returns the value with IEnumerable<ProfileVM> type, your LINQ statement is expected to return set of ProfileVM(s) [concrete type] as below:

public IEnumerable<ProfileVM> GetEmployees()
{
    var register2 = from a in context.Employees
                    join b in context.Accounts on a.NIK equals b.NIK
                    select new ProfileVM
                    {
                        Nik = a.NIK,
                        Password = b.Password
                    };
    return register2.ToList();
}

CodePudding user response:

instead of

public IEnumerable<ProfileVM> GetEmployees()
    {
        var register2 = from a in context.Employees
                        join b in context.Accounts on a.NIK equals b.NIK
                        select new
                        {
                            a.NIK,
                            b.Password,
                        };
        return register2.ToList();
    }

Try

public IEnumerable<ProfileVM> GetEmployees()
    {
        var register2 = from a in context.Employees
                        join b in context.Accounts on a.NIK equals b.NIK
                        select new ProfileVM()
                        {
                            NIK = a.NIK,
                            Password = b.Password,
                        };
        return register2.ToList();
    }
  • Related