gpt4 book ai didi

c# - MongoDB Driver Query * Filter Definition Builder * Nin $nin 不在过滤器中

转载 作者:可可西里 更新时间:2023-11-01 09:29:06 26 4
gpt4 key购买 nike

有没有人使用过 C# .Net MongoDB Driver FilterDefinitionBuilder's not in filter?这是我放在一起的一个简单示例,但我似乎无法开始工作。假设我们必须保持集合的原样。目标是仅检索不在指定 ArtistGenresDocument 列表中的 ArtistDetailsDocument。

代码将无法编译并声明“无法将 lambda 表达式转换为类型‘MongoDB.Driver.FieldDefinition ArtistDetailsDocument,ArtistGenresDocument’,因为它不是委托(delegate)类型”。

感谢帮助!\m/\m/

public class ArtistDetailsDocument
{
public ObjectId Id { get; set; }
public String artist_ID { get; set; }
public String artistName { get; set; }
}

public class ArtistGenresDocument
{
public ObjectId Id { get; set; }
public String artist_ID { get; set; }
public String genre { get; set; }
}

IEnumerable<ArtistGenresDocument> list = await ArtistGenresCollection.Find(x => x.genre == "Pop" | x.genre == "Easy Listening").ToListAsync();

var filter = Builders<ArtistDetailsDocument>.Filter.Nin<ArtistGenresDocument>(x => x.artist_ID, list);

var ArtistDetailsDocuments = ArtistDetailsCollection.Find(filter);

最佳答案

这是在 pieperu 的帮助下的解决方案!

IEnumerable<ArtistGenresDocument> list = await ArtistGenresCollection
.Find(x => x.genre == "Pop" || x.genre == "Easy Listening")
.ToListAsync();

var filter = Builders<ArtistDetailsDocument>
.Filter
.Nin(x => x.artist_ID, list.Select(l => l.artist_ID));

var ArtistDetailsDocuments = await ArtistDetailsCollection
.Find(filter)
.ToListAsync();

public class ArtistDetailsDocument
{
public ObjectId Id { get; set; }
public String artist_ID { get; set; }
public String artistName { get; set; }
}

public class ArtistGenresDocument
{
public ObjectId Id { get; set; }
public String artist_ID { get; set; }
public String genre { get; set; }
}

关于c# - MongoDB Driver Query * Filter Definition Builder * Nin $nin 不在过滤器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com