微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LiteDB查询JOIN和IN以获取相关数据

如何解决LiteDB查询JOIN和IN以获取相关数据

我正在使用LiteDB构建一个非常简单的博客,但无法通过Blog标签获取查询...

目前,我只有两个简单的对象,一个Article一个Tag

public class Tag
    {
        public int ID { get; set; }
        public string name { get; set; }
    }
    // https://github.com/mbdavid/LiteDB/issues/1638
public class Article {
    public int ID { get; set; }
    public long DateTime { get; set; } = System.DateTime.Now.ToUnix();
    public long LastEdit { get; set; } = System.DateTime.Now.ToUnix();
    public string title { get; set; }
    public string url => String.Concat("/Blog/",this.title.ValidFileName(),"-",this.ID.ToString());
    public string article { get; set; }
    public int authorId { get; set; }
    public string authorName { get; set; }
    public bool publish { get; set; }
    public string[] images { get; set; }
    [BsonRef("tags")]
    public Tag[] Tags { get; set; }
}

使用此代码,我可以令人满意地Insert博客及其相应的标签

注意;关于Article.ID,Article.title以及Tag.ID和Tag.name的索引

    public static void Insert(Blog.Article Article)
    {
        // Open database (or create if doesn't exist)
        using (var db = new LiteDatabase(connection))
        {

            ILiteCollection<Blog.Tag> Tags = db.GetCollection<Blog.Tag>("tags");
            try
            {
                Tags.EnsureIndex(p => p.ID,true);
                Tags.EnsureIndex(p => p.name,true);
            }
            catch { }
            if (Article.Tags != null)
            {
                foreach (Blog.Tag Tag in Article.Tags)
                {
                    Tag.ID = Tags.Insert(Tag);
                }
            }

            // Get a collection (or create,if doesn't exist)
            ILiteCollection<Blog.Article> Articles = db.GetCollection<Blog.Article>("articles");
            try
            {
                Articles.EnsureIndex(p => p.ID,true);
                Articles.EnsureIndex(p => p.title,true);
            }
            catch { }

            // Insert new Article (Id will be auto-incremented)
            Articles.Insert(Article);
        }
    }

我可以使用接受可选查询值的辅助函数获取文章

    public static List<Blog.Article> GetArticles(int requestedId = 0,int authorid = 0,List<int> TagList = null,string start = "",string end = "")
    {
        long startTime = start == "" ? 0 : Convert.ToDateTime(start).ToUnix();
        long endTime = end == "" ? 0 : Convert.ToDateTime(end).ToUnix();
        using (var db = new LiteDatabase(connection))
        { 
            //// Use LINQ to query documents (filter,sort,transform) 
            return db.GetCollection<Blog.Article>("articles") 
                .Include(x => x.Tags)
                .Find(a => (requestedId == 0 || requestedId == a.ID) &&
                    (authorid == 0 || authorid == a.authorId) &&
                    (startTime == 0 || startTime <= a.DateTime) &&
                    (endTime == 0 || endTime >= a.DateTime)
                .OrderByDescending(x => x.DateTime)
                .ToList();
        }            
    }

但是,对我而言,我无法弄清楚如何添加查询获取带有Give标签标签文章

我尝试使用:

a.Tags.Select(a=>a.ID).Any(x => TagList.Any(y => y == x))  

Linq中的嵌套Any适用于其他内容,但是为此,我得到了NullReferenceException: Object reference not set to an instance of an object.

我不确定在这种情况下null引用在哪里,我怀疑这是由于LiteDB操作JOINs造成的,也许此时没有商品标签数据?

我尝试了诸如此类的不同变体

 a.Tags.Select(x => x.ID).Intersect(TagList).Any())

这会导致错误Method Intersect(IEnumerable<T>) in Enumerable are not supported when convert to BsonExpression

因此,对于tiem,我不得不回退以仅允许查询单个标签,但是我想要的是(伪代码"SELECT * FROM articles LEFT JOIN tags WHERE tags.id IN (*mylist comma separated*) 类型查询,这是我在sql中使用的查询

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。