Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ返回子对象_C#_Entity Framework_Linq_Asp.net Core - Fatal编程技术网

C# 使用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;

我正在学习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; }
    }
                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

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

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()
没问题很乐意帮助