How to store entities context query object into ge

2019-09-16 18:42发布

In my last question, i successfully to dump the data into combo box with solution from Leo but now, i want to get everything of specific user and store into generic list. i tried with this code but it's not working. May i know is there something wrong with my code ?

  var contexti = new OrganizationContext();
  //contexti.Load(contexti.GetAdminPassQuery(userName), LoadInfo, null);
  EntityQuery query = contexti.GetAdminPassQuery(userName);
  contexti.Load<tblAdmin>(query).Completed += (sender, args) =>
           {
                  List<AdminInfo> list = ((LoadOperation<tblAdmin>) sender).Entities.
                                         ToList();}

this is in the domain context.

    public IQueryable<tblAdmin>  GetAdminPass(string adminId)
    {
        return this.ObjectContext.tblAdmins.Where(e => e.adminID.Equals(adminId));
    }

this is the generic list store in helper folder

    public class AdminInfo
{
    public string AdminId { get; set; }
    public string AFirstName { get; set; }
    public string ALastName { get; set; }
    public string AEmail { get; set; }
    public string APassword { get; set; }
}

Thank you for reply

1条回答
放我归山
2楼-- · 2019-09-16 19:11

AdminInfo is not the same type as tblAdmin so you need to manually create a new list:

contexti.Load<tblAdmin>(query).Completed += (sender, args) =>
{
    List<AdminInfo> list = new List<AdminInfo>();

    //for each entity returned, create a new AdminInfo and add it to the list
    foreach (var item in ((LoadOperation<Car>)sd).Entities)
    {
        list.Add(new AdminInfo(){ AdminId = item.AdminId, AFirtsName = item.FirstName /*other properties*/});
    }

}
查看更多
登录 后发表回答