Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for selectmany in ravendb using client api

Tags:

ravendb

I have a ravendb class like such:


        public class Student
        {
            public string Id { get; set; }
            public string TopLevelProperty { get; set; }
            public Dictionary&ltstring, string&gt Attributes { get; set; }
            public Dictionary&ltstring,List&ltDictionary&ltstring, string&gt&gt&gt CategoryAttributes { get; set; }
        }

and a document like so:
enter image description here

The following linq will not work due to a selectmany:


                test = (from student in session.Query()
                        from eduhistory in student.CategoryAttributes["EducationHistory"]
                        where eduhistory["StartYear"] == "2009"
                              select student).ToList();

How can I get all students where StartYear == 2009?

like image 314
basarat Avatar asked Oct 12 '22 02:10

basarat


2 Answers

This does it :


test = session.Advanced.LuceneQuery()
            .Where("CategoryAttributes.EducationHistory,StartYear:2009")
            .ToList();

Notice the comma rather than a dot after EducationHistory. This indicates that we are looking at the list to find a property in one of the items named StartYear. If I wanted greater than :


test = session.Advanced.LuceneQuery()
            .Where("CategoryAttributes.EducationHistory,StartYear:[2009 TO null]")
            .ToList();

etc etc.

like image 95
basarat Avatar answered Nov 25 '22 17:11

basarat


This should work:

test = (from student in session.Query()
       where student.CategoryAttributes["EducationHistory"].Any(edu => edu["StartYear"]== "2009" )
       select student).ToList();
like image 35
Ayende Rahien Avatar answered Nov 25 '22 18:11

Ayende Rahien