Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<DAL.HRM_PersonalInformations>'

I am trying to Get some data from Database and Bind them in Drop-Down list.. But getting following error : -

public virtual List<HRM_PersonalInformations>GetAssignePerson(String OCODE, int dptID){
 var query = (context.HRM_PersonalInformations.Where
             (c => c.OCODE == OCODE && c.DepartmentId == dptID)
             .Select(c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID})).ToList();

  return query; // It indicate error line
}

After that I am trying to Bind data in dropdown list and my code is following : -

private void FillAssignPerson()
 {
   try
     {
            string OCODE = ((SessionUser)Session["SessionUser"]).OCode;
            int dptId = Convert.ToInt32(ddlAssignPerDept.SelectedValue);

            var row = enquiryBll.GetAssignePerson(OCODE, dptId).ToList();

            //const string list = "SELECT FirstName + ' ' + LastName AS FullName, EID, FirstName, LastName " +
            //                    "FROM HRM_PersonalInformations " +
            //                    "ORDER BY EID ASC"
            if (row.Count > 0)
            {
                ddlAssignPerson.Items.Clear();
                ddlAssignPerson.DataSource = row;
                ddlAssignPerson.DataTextField = "FullName";
                ddlAssignPerson.DataValueField = "EID";
                ddlAssignPerson.DataBind();
                ddlAssignPerson.Items.Insert(0, new ListItem("----- Select One -----", "0"));
                ddlAssignPerson.AppendDataBoundItems = false;
            }
        }

Is it right way ?? Can anyone help me ? Thanks for Advance ..

Jon Skeet
people
quotationmark

Well, in your projection you have:

c => new {FullName = (c.FirstName + ' ' + c.LastName), c.EID}

That's creating an instance of an anonymous typoe - not an instance of HRM_PersonalInformations. If those are the only two properties you need in HRM_PersonalInformations, you could just change it to:

c => new HRM_PersonalInformations {
   FullName = (c.FirstName + ' ' + c.LastName), 
   EID = c.EID
}

Or judging by your query, you may not need a projection at all. You may be fine with:

return context.HRM_PersonalInformations
              .Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
              .ToList();

Alternatively, if you only need those properties and don't really need the items of your list to be instances of HRM_PersonalInformations, you could just change the method's signature to be:

public virtual IList GetAssignePerson(...)

and keep the body as it is. You can't explicitly write a method declaration which specifies the anonymous type, because it doesn't have a name - but List<T> implements IList, so the above will certainly compile... it will fail if you later try to cast an element in it to HRM_PersonalInformations though.

EDIT: If the string concatenation is a problem, you might need to do that client-side. For example:

public IList GetAssignePerson(String OCODE, int dptID) {
    return context.HRM_PersonalInformations
                  .Where(c => c.OCODE == OCODE && c.DepartmentId == dptID)
                  .Select(c => new { c.FirstName, c.LastName, c.EID })
                  .AsEnumerable() // Do the rest client-side
                  .Select(c => new {
                      FullName = c.FirstName + " " + c.LastName,
                      c.EID
                  })
                  .ToList();
}

people

See more on this question at Stackoverflow