gpt4 book ai didi

c# - 尝试比较 mongodb 字符串数组中的标签,文本框输入 c#

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:02 25 4
gpt4 key购买 nike

好吧,我有一个 mongodb,它有一个名为视频的集合,在视频中我有一个名为标签的字段。我想要做的是将文本框输入与集合中所有视频的标签进行比较,如果标签与文本框的输入匹配,则将它们返回到 gridview。当我创建一个新视频时,标签字段是一个字符串数组,因此可以存储多个标签。我正在尝试在 c# 中执行此操作。希望你们中的一些人能帮忙谢谢!

创建新视频文档的代码。

        #region Database Connection
var client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("Database");
#endregion

var videos = db.GetCollection<Video>("Videos");
var name = txtVideoName.Text;
var location = txtVideoLocation.Text;
var description = txtVideoDescription.Text;
var user = txtVideoUserName.Text;
string[] lst = txtVideoTags.Text.Split(new char[] { ',' });

var index = videos.Count();
var id = 0;

if (id <= index)
{
id += (int)index;
}

videos.CreateIndex(IndexKeys.Ascending("Tags"), IndexOptions.SetUnique(false));

var newVideo = new Video(id, name, location, description, lst, user);

videos.Insert(newVideo);

好的,这就是搜索方法的样子,我刚刚使语法与 Grant Winney 回答的有点不同。

 var videos = db.GetCollection<Video>("Videos");
string[] txtInput = txtSearchTags.Text.Split(new char[] { ',' });

var query = (from x in videos.AsQueryable<Video>()
where x.Tags.ContainsAny(txtInput)
select x);

最佳答案

假设 MongoDB 驱动程序可以将其正确转换为有效查询,这会找到所有标签包含 TextBox 中指定标签的视频。

var videos = db.GetCollection<Video>("Videos")
.AsQueryable()
.Where(v => v.Tags.Split(',')
.ContainsAny(txtVideoTags.Text.Split(',')))
.ToList();

确保您在顶部有 using MongoDB.Driver.Linq;

关于c# - 尝试比较 mongodb 字符串数组中的标签,文本框输入 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318939/

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