Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# Ef核心:包含Where子句_C#_Linq_Ef Core 2.1 - Fatal编程技术网

C# Ef核心:包含Where子句

C# Ef核心:包含Where子句,c#,linq,ef-core-2.1,C#,Linq,Ef Core 2.1,我有这种型号 public class Blog { public IList<Post> Posts { get; set; } } public class Post { public PostType PostType { get; set; } } public class PostType { public string Code { get; set; } // "Code1" and "Code2" } 这个查询的问题是;如果博客中有类型为Cod

我有这种型号

public class Blog
{
   public IList<Post> Posts { get; set; }
}

public class Post
{
   public PostType PostType { get; set; }
}

public class PostType
{
   public string Code { get; set; } // "Code1" and "Code2"
}
这个查询的问题是;如果博客中有类型为Code1Code2的帖子,那么上面的查询将同时包含codeCode2Code1两个帖子,因为我使用的是任意一个

所以我尝试了这个:我用了所有的,而不是任何的

但在上述场景中,此查询不返回任何结果

在给定的情况下,是否有一种方法可以使用单个Ef linq查询返回所有帖子类型为Code1的博客,而不包括帖子类型Code2

编辑:
找到这个博客

该解决方案与Thierry V的类似,只是将过滤后的帖子存储在一个单独的字典中,以避免EF跟踪的副作用:

然而,我真的看不出这种代码背后的原因。通常,您会抓取所有满足您条件的博客(包含Code1的任何帖子),然后根据需要过滤它们

var blogs = _dbContext.Blogs.Include(b => b.Posts).ThenInclude(b => b.PostType)
    .Where(b => b.Posts.Count == 0 || b.Posts.Any(p => p.PostType.Code == "Code1")
    .ToList(); 

// Storing the filterd posts in a dictionary to avoid side-effects of EF tracking.
var dictionary = new Dictionary<int, List<Post>>();
foreach (var blog in blogs) {
    dictionary[blog.BlogId] = blog.Posts.Where(p => p.PostType.Code == "Code1").ToList();
}
var blogs=\u dbContext.blogs.Include(b=>b.Posts)。然后Include(b=>b.PostType)
。其中(b=>b.Posts.Count==0 | | b.Posts.Any(p=>p.PostType.Code==“Code1”)
.ToList();
//将filterd帖子存储在字典中以避免EF跟踪的副作用。
var dictionary=newdictionary();
foreach(博客中的var博客){
dictionary[blog.BlogId]=blog.Posts.Where(p=>p.PostType.Code==“Code1”).ToList();
}

您模型中的博客没有PostType,而是一列每个都有PostType的帖子。因此,是否要返回所有有PostType Code1帖子的博客,或者返回有PostType Code1帖子的博客,但只返回PostType Code1帖子?您的问题不太清楚。一个博客可以包含这两种类型的帖子?因此,如果有一个blog有两种类型,您希望它返回还是否?如果是,您希望它返回所有帖子还是特定类型的帖子。在我看来,您需要两个查询,一个是获取所有包含任意混合的博客。第二个是从第一个查询中仅筛选出特定类型的帖子。@AsheraH我希望返回所有包含类型帖子列表的博客代码1。如果只获取所有博客及其所有帖子,其中代码!=Code2,这不是更简单吗?@Archer要做到这一点,您必须使用任何…对吗?仍然会出现相同的结果
_dbContext.Blogs.Include(b => b.Posts).ThenInclude(b => b.PostType)
    .Where(b => b.Posts.Count == 0 || b.Posts.All(p => p.PostType.Code == "Code1").ToList();
var blogs = _dbContext.Blogs.Include(b => b.Posts).ThenInclude(b => b.PostType)
    .Where(b => b.Posts.Count == 0 || b.Posts.Any(p => p.PostType.Code == "Code1").ToList(); 
// blogs contains posts which have Code1, and maybe Code2

//filter the posts by assigning only the posts with Code1
blogs.ForEach(b=> b.Posts = b.Posts.Where( p => p.PostType.Code == "Code1"))
var blogs = _dbContext.Blogs.Include(b => b.Posts).ThenInclude(b => b.PostType)
    .Where(b => b.Posts.Count == 0 || b.Posts.Any(p => p.PostType.Code == "Code1")
    .ToList(); 

// Storing the filterd posts in a dictionary to avoid side-effects of EF tracking.
var dictionary = new Dictionary<int, List<Post>>();
foreach (var blog in blogs) {
    dictionary[blog.BlogId] = blog.Posts.Where(p => p.PostType.Code == "Code1").ToList();
}