Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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
.net 在实体框架中过滤多对多关系中的数据_.net_Entity Framework_Linq_Asp.net Core_Many To Many - Fatal编程技术网

.net 在实体框架中过滤多对多关系中的数据

.net 在实体框架中过滤多对多关系中的数据,.net,entity-framework,linq,asp.net-core,many-to-many,.net,Entity Framework,Linq,Asp.net Core,Many To Many,我的项目中有一个简单的多对多关系模型 以下是我的课程: public class AppUser{ public string Id { get; set; } public string Name { get; set; } public List<AppUserBlogs> AppUserBlogs { get; set; } } 公共类AppUser{ 公共字符串Id{get;set;} 公共字符串名称{get;set;} 公共列表AppUserBlogs

我的项目中有一个简单的多对多关系模型

以下是我的课程:

public class AppUser{
   public string Id { get; set; }
   public string Name { get; set; }

   public List<AppUserBlogs> AppUserBlogs { get; set; }
}
公共类AppUser{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共列表AppUserBlogs{get;set;}
}
公共类博客{
公共字符串Id{get;set;}
公共字符串标题{get;set;}
公共字符串体{get;set;}
公共列表AppUserBlogs{get;set;}
}
好的,我想在这里实现的是,我想检索所有的博客,但是用户已经拥有的博客

我可以做相反的事情,那就是检索特定用户的博客列表

public List<Blog> GetBlogsByUser(AppUser user)
{
     using (var context = new BlogContext())
     {
         var blogs = context.Blogs.AsQueryable();

         if (!string.IsNullOrEmpty(user.Id))
         {
               blogs =  blogs
                           .Include(i => i.AppUserBlogs)
                           .ThenInclude(i => i.AppUser)
                           .Where(i => i.AppUserChasses.Any(a => a.AppUser.Id == user.Id));
         }

         return blogs.ToList();
    }
}
公共列表GetBlogsByUser(AppUser)
{
使用(var context=new BlogContext())
{
var blogs=context.blogs.AsQueryable();
如果(!string.IsNullOrEmpty(user.Id))
{
博客=博客
.Include(i=>i.AppUserBlogs)
.ThenInclude(i=>i.AppUser)
其中(i=>i.AppUserChasses.Any(a=>a.AppUser.Id==user.Id));
}
return blogs.ToList();
}
}
实际上,我需要与这种方法相反的方法。例如,假设我的Blogs表中有20个blog。其中4个是用户1的博客。(BlogId-1->UserId-1 | BlogId-2->UserId-1等)

因此,在另一个视图中,我想向User-1展示User-1没有的其他16个博客

我希望我问得很清楚


谢谢你抽出时间

只要否定where子句:

.Where(i => !i.AppUserChasses.Any(a => a.AppUser.Id == user.Id));

我确实解决了这个问题,把方法改成了这个。但我仍然不确定这是否是最好的解决方案

public List<Blog> GetAllExceptUserBlogs(AppUser user)
{
     using (var context = new BlogContext())
     {                
         var allBlogs = context.Blogs
                               .Include(i => i.AppUserBlogs)
                               .ThenInclude(i => i.AppUser)
                               .ToList();
         var selectedUser = context.AppUsers
                                   .Include(i => i.AppUserBlogs)
                                   .ThenInclude(i => i.Blog)
                                   .Where(i => i.Id == user.Id)
                                   .FirstOrDefault();
         List<Blog> tempBlogList = new List<Blog>();

         foreach (var blog in allBlogs)
         {
              if(!selectedUser.AppUserBlogs.Select(i => i.Blog).Contains(blog))
              {
                   tempBlogList.Add(blog);
              }
         }

         return tempBlogList;               
    }
 }
公共列表GetAllExceptUserBlogs(AppUser)
{
使用(var context=new BlogContext())
{                
var allBlogs=context.Blogs
.Include(i=>i.AppUserBlogs)
.ThenInclude(i=>i.AppUser)
.ToList();
var selectedUser=context.AppUsers
.Include(i=>i.AppUserBlogs)
.然后包括(i=>i.Blog)
.Where(i=>i.Id==user.Id)
.FirstOrDefault();
List tempBlogList=新列表();
foreach(所有博客中的var博客)
{
如果(!selectedUser.AppUserBlogs.Select(i=>i.Blog).Contains(Blog))
{
添加(blog);
}
}
返回临时日志列表;
}
}

你好,克里斯,我试过了,实际上这不是我想要的结果。因为这是一个多对多的关系,假设User-2也有相同的博客,那么它也会显示这4个博客,但是我确实改变了这个方法,你能看一下吗?它奏效了,但我不确定这是否是最好的解决方案。。。
public List<Blog> GetAllExceptUserBlogs(AppUser user)
{
     using (var context = new BlogContext())
     {                
         var allBlogs = context.Blogs
                               .Include(i => i.AppUserBlogs)
                               .ThenInclude(i => i.AppUser)
                               .ToList();
         var selectedUser = context.AppUsers
                                   .Include(i => i.AppUserBlogs)
                                   .ThenInclude(i => i.Blog)
                                   .Where(i => i.Id == user.Id)
                                   .FirstOrDefault();
         List<Blog> tempBlogList = new List<Blog>();

         foreach (var blog in allBlogs)
         {
              if(!selectedUser.AppUserBlogs.Select(i => i.Blog).Contains(blog))
              {
                   tempBlogList.Add(blog);
              }
         }

         return tempBlogList;               
    }
 }