0

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.

Beatles1692
  • 5,214
  • 34
  • 65
  • At least put valid json if you want some help. – mybirthname Dec 11 '16 at 12:31
  • It's a sample json document. I had some typo but I don't think that it's relevant to the question itself. I would correct it. – Beatles1692 Dec 11 '16 at 12:34
  • It is relevant if you want someone to reproduce your problem – mybirthname Dec 11 '16 at 12:34
  • Can you put a breakpoint at `context.Principles` and see if it returns any objects, and whether those objects contain expected data? I think it's not wise start the question with "Any() is not working" because there is zero chance that the problem is in the Any() function. – Zoran Horvat Dec 11 '16 at 12:38
  • Yes the problem is not in Any. I re create your code and there is no problem. – mybirthname Dec 11 '16 at 12:40
  • Here you can check it :https://dotnetfiddle.net/pZPKNv – mybirthname Dec 11 '16 at 12:42
  • @mybirthname I am using brightstardb to store my documents. – Beatles1692 Dec 11 '16 at 12:46
  • @ZoranHorvat Yes I have checked that I am getting all Principles if I omit the Any part I also can search Principles using its text property without any problem. – Beatles1692 Dec 11 '16 at 12:48
  • 1
    Are you using a Ling provider specifically for brightstardb? If so I'd suggest that you determine the SQL, or I guess in this case NoSql that your linq query is transformed into to help determine the issue. Usually you can do that by calling `ToString` on the query (before calling `ToList` on it). – juharr Dec 11 '16 at 15:02
  • @juharr brightstardb offers its own Linq provider out of the box. – Beatles1692 Dec 12 '16 at 10:22

2 Answers2

0

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

yan yankelevich
  • 885
  • 11
  • 25
  • I have solved the problem somehow by converting history from string into IHistory. I will post my solution as an answer soon. – Beatles1692 Dec 12 '16 at 10:21
0

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);
            }
        }
    }
}
Kal
  • 1,887
  • 12
  • 16