RavenDB:如何编写关于孙辈的查询

本文关键字:于孙辈 查询 何编写 RavenDB | 更新日期: 2023-09-27 18:27:36

给定下面的C#代码,我正在尝试检索所有省份所在的大陆,这些省份的城市包括:

  1. 名称和地址分别设置为"Aloma"answers"123"的城市,以及
  2. 另一个名称和地址分别设置为"赫曼"answers"435"的城市

public class Continent
{
    public string Name { get; set; }
    public List<Country> Countries{ get; set; }
}
public class Countries
{
    public string Name { get; set; }
    public List<Province> Provinces{ get; set; }
}
public class Province
{
    public string Name { get; set; }
    public List<Province> Cities { get; set; }
}
public class City
{
    public string Name { get; set; }
    public string Address   { get; set; }
}

我尝试过使用下面的查询,但它似乎不起作用。你能帮帮我吗?

Expression<Func<Province, bool>> matchCities = rec =>
      (rec.Cities.Count(fi => fi.Name == "Aloma" && fi.Address== "123") > 0)
        && (rec.Cities.Count(fj => fj.Name == "Hemane" && fj.Address== "435") > 0);
Func<Province, bool> funcMatchCities= matchCities.Compile();
var results3 = session.Query<Continent>()
                      .Where(top => top.Countries.Any(ta => ta.Province.Any(
                                  rec => funcMatchCities(rec))))
                      .OfType<Continent>()
                      .ToList();

RavenDB:如何编写关于孙辈的查询

您可以这样查询:

var results3 = session.Query<Continent>()
                      .Where(top => top.Countries.Any(ta => ta.Province.Any(
                                 rec =>
      (rec.Fields.Any(fi => fi.Name == "Aloma" && fi.Address== "123") )
        && (rec.Fields.Any(fj => fj.Name == "Hemane" && fj.Address== "435") )))
                      .OfType<Continent>()
                      .ToList();

请注意,在将它们发送到linq提供程序时,不能使用Compile表达式。