简体   繁体   中英

Any() is not working in Brightstardb

I have a document like this:

    {
       "Document":{
                  "Principles":[{"text":"Text","history":["Text1","Text2","Text3"]}]
                  }
    }

I would like to search for all Principles that contain a history that contains "Text".

I have 2 interfaces like this :

[Entity]
public interface IDocument
{
   string Id{get;}

   ICollection<IPrinciple> Principles{get;set;}
}

[Entity]
public interface IPrinciple
{
   string Id{get;}

   ICollection<string> history{get;set;}

   string text{get;set;}
}

Here's what I did :

using(var context=new MyEntityContext(connectionString))
{
     var principles=(from p in context.Principles where p.history.Any(h=>h.Contains("Text")) select p).ToList();
}

But I am getting a list that contains no principle.

If you try to write it this way :

 var principles= _context.Principles.Any(p => p.history.Contains("Text"));

You will get the following error :

NotSupportedException : LINQ-to-SPARQL does not currently support the result operator 'Any()'

So your guess was right, currently brightstarDB doesn't seems to support the Any() operation.

You always can replace your Any with a Where and some tweaks to obtain a similar (but working) result

I don't think that the Any() is the problem. This program works just fine, returning the expected 2 principles from the query. I'm just using the IPrinciple and IDocument interfaces exactly as you posted in your original question, and the query is also exactly as you posted originally.

class Program
{
    private static readonly string ConnectionString = "type=embedded;storesDirectory=brightstardb;storeName=anytest";
    static void Main(string[] args)
    {
        SetupStore();
        QueryStore();
    }

    private static void SetupStore()
    {
        if (!Directory.Exists("brightstardb"))
        {
            Directory.CreateDirectory("brightstardb");
            using (var context = new MyEntityContext(ConnectionString)
            )
            {
                var doc1 = context.Documents.Create();
                var doc2 = context.Documents.Create();
                var doc3 = context.Documents.Create();
                var p1 = context.Principles.Create();
                p1.History = new List<string> {"Strings", "of", "Text"};
                var p2 = context.Principles.Create();
                p2.History = new List<string> {"Nothing", "To See Here", "Move Along"};
                var p3 = context.Principles.Create();
                p3.History = new List<string> { "Another", "Principle containing Text"};
                doc1.Principles = new List<IPrinciple> {p1};
                doc2.Principles = new List<IPrinciple> {p2};
                doc3.Principles = new List<IPrinciple> {p3};
                context.SaveChanges();
            }
        }
    }

    private static void QueryStore()
    {
        using (var context = new MyEntityContext(ConnectionString))
        {
            var principles = (from p in context.Principles where p.History.Any(h => h.Contains("Text")) select p).ToList();
            foreach (var p in principles)
            {
                Console.WriteLine("Found principle: {0}", p.Id);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM