C# 使用LINQ返回子对象
我正在学习EF Core和LINQ中的多对多联合表。我有以下型号:C# 使用LINQ返回子对象,c#,entity-framework,linq,asp.net-core,C#,Entity Framework,Linq,Asp.net Core,我正在学习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;
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; }
}
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);
}
}
List userList=newlist();
foreach(项目经理项目经理)
{
//这会导致错误,无法将字符串userId转换为char??
foreach(pm.AppUserID中的字符串userId)
{
AppUser newUser=await userManager.FindByIdAsync(userId);
userList.Add(newUser);
}
}
因为你的pm.AppUserID
已经是一个字符串值,如果你在上面使用foreach
你将迭代器字符串值,那么你将得到char
从您的评论来看,您似乎可以使用linqSelect
List<AppUser> userList = proj.ProjectManagers.Select(x=>x.AppUser).ToList();
List userList=proj.ProjectManagers.Select(x=>x.AppUser.ToList();
我认为这一定很接近,但是我得到了一个错误无法将类型System.Collections.Generic.List隐式转换为System.Linq.IQueryable
我还没有找到FindByIdAsync
来使用我的项目管理器
类。是否可以执行repository.ProjectManagers.Where(…)
调用?我已经用Projectmanagers
数据设置了我的dbContext,或者您可以使用repository.Projectmanagers.Select(x=>x.AppUser.ToList()
没问题很乐意帮助