代码之家  ›  专栏  ›  技术社区  ›  coolhand

使用LINQ返回子对象

  •  0
  • coolhand  · 技术社区  · 6 年前

        public class ProjectManager
        {
            public int ProjectID { get; set; }
            public Project Project { get; set; }
    
            public string AppUserID { get; set; }
            public AppUser AppUser { get; set; }
        }
    

    我有以下视图模型:

        public class DeleteUserFromProjectViewModel
        {
            public IQueryable<AppUser> Users { get; set; }
            public PagingInfo PagingInfo { get; set; }
        }
    

    Users 对于我的ViewModel:

                    DeleteUserFromProjectViewModel model = new DeleteUserFromProjectViewModel
                    {
                        //HOW DO I RETURN JUST THE APPUSERS FROM THE PROJECT MANAGER OBJECT?
                        //Users = repository.ProjectManagers.Any(p => p.ProjectID == projectId).Select;
    
                        PagingInfo = new PagingInfo
                        {
                            CurrentPage = page,
                            ItemsPerPage = PageSize,
                            TotalItems = proj.ProjectManagers.Count()
                        }
    
                    };
    

    我已经试过了

                    List<AppUser> userList = new List<AppUser>();
                    foreach(ProjectManager pm in proj.ProjectManagers)
                    {
                        //this gives error that cannot convert string userId to char??
                        foreach (string userId in pm.AppUserID)
                        {
                            AppUser newUser = await userManager.FindByIdAsync(userId);
                            userList.Add(newUser);
                        }
                    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   D-Shih    6 年前

    因为你的 pm.AppUserID 已经是字符串值,如果使用 foreach char

    从你的评论来看,你似乎可以使用linq Select .

    List<AppUser> userList = proj.ProjectManagers.Select(x=>x.AppUser).ToList();
    
    推荐文章