How to store entities context query object into generic list?

为君一笑 提交于 2019-12-12 03:03:51

问题


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:


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*/});
    }

}


来源:https://stackoverflow.com/questions/8018174/how-to-store-entities-context-query-object-into-generic-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!