我正在学习EF Core和LINQ中的多对多联合表。我有以下模型:
public class ProjectManager
{
public int ProjectID { get; set; }
public Project Project { get; set; }
public string AppUserID { get; set; }
public AppUser AppUser { get; set; }
}
我有以下ViewModel:
public class DeleteUserFromProjectViewModel
{
public IQueryable<AppUser> Users { get; set; }
public PagingInfo PagingInfo { get; set; }
}
在我的控制器中,我尝试为ViewModel填充Users
:
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()
}
};
我尝试过kludge
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);
}
}
答案 0 :(得分:3)
因为您的pm.AppUserID
已经是一个字符串值,所以如果在其上使用foreach
,则将迭代字符串值,那么您将得到char
根据您的评论,您似乎可以使用linq Select
。
List<AppUser> userList = proj.ProjectManagers.Select(x=>x.AppUser).ToList();