Workaround for selectmany in ravendb using client api

对着背影说爱祢 提交于 2019-12-22 06:38:04

问题


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:


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?


回答1:


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.




回答2:


This should work:

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


来源:https://stackoverflow.com/questions/5973637/workaround-for-selectmany-in-ravendb-using-client-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!