我需要执行一个查询来检查一个集合是否在给定集合中,就像常规操作一样,但对于集合。
class Post
{
public string[] Tags {get;set;}
}
session.Queury<Post>.Where(x=>x.Tags.in(new[]{".net","c#","RavenDB"})).ToList();
所以如果我在我的数据库中有:
new Post{Tags= new[]{"C#",".net"}};
它会被退回
但如果我有:
new Post{Tags= new[]{"C#",".net","SQLServer"}};
不会退还。
更新:
我想做的是:
session.Query<Post>()
.Where(x => x.Tags.All(y => y.In(new[] { "C#", ".net", "RavenDB" })))
.ToList();
但我遇到了 System.NotSupportedException。
最佳答案
我设法找到解决方案:
static void Main(string[] args)
{
var sessionStore = new EmbeddableDocumentStore
{
RunInMemory = true,
UseEmbeddedHttpServer = true,
Conventions =
{
DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite
}
};
sessionStore.Initialize();
using (var session = sessionStore.OpenSession())
{
var allTags = new[] {"C#", ".net", "RavenDB", "Linux", "Mac"};
var tagsCollection = new[] {"C#", ".net", "RavenDB"};
var complementTagsCollection = allTags.Except(tagsCollection).ToList();
session.Store(new Post
{
Tags = new List<string>{"C#",".net"}
});
session.SaveChanges();
// Posts where all their tags are in tagsCollection
var result = session.Query<Post>().Where(x => !x.Tags.In(complementTagsCollection)).ToList();
}
}
关于c# - RavenDB Collection "in"集合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25535363/