var database = new MongoClient("mongodb://192.168.3.122").GetDatabase("TestDb");
var collection = database.GetCollection<Post>("Post");
var res = collection.AsQueryable().Where(x => x.Id.In(new List<int> { 1, 2, 3 })).Select(y => y).ToList(); //error here
foreach (var post in res)
{
Console.WriteLine(post.Id + " " + post.Code);
}
我想要与此SQL查询类似的结果:
Select * From Post Where Id In(1, 2, 3)
答案 0 :(得分:1)
试试这个
var Ids = new int[] { 1, 2, 3};
var res = collection.AsQueryable().Where(x => Ids.Contains(x.Id)).Select(y => y).ToList();
答案 1 :(得分:0)
我认为你可以像这样使用In
过滤器:
var filter = Builders<Post>.Filter.In(f=> f.Id, new []{ 1, 2, 3 });
获得预期结果:
// As an Async way
var res = collection.Find(filter).ToListAsync().Result;
// As a Sync way
var res = collection.Find(filter).ToList();