List<>上的LINQ to NHibernate表达式

时间:2011-02-14 17:28:13

标签: c# linq nhibernate linq-to-nhibernate

我正在使用LINQ到NHibernate,并且有一个看起来像这样的模型(简化):

public class Person {
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address {
    public virtual string Street { get; set; }
    public virtual string City { get; set; }
}

我可以执行以下LINQ to NHib查询:

Expression<Func<Person, bool>> predicate = pr => pr.FirstName == "Bob";
List<Person> people = session.Query().Where(predicate).ToList();

但我一直试图让所有拥有City ==“Something”地址的人回归。

1 个答案:

答案 0 :(得分:4)

怎么样:

List<Person> people = session.Query()
                        .Where(p => p.Addresses.Any(a => a.City == "Something"))
                        .ToList();

假设您希望查询仍在数据库中执行。如果您只想在已经返回的List<Person>内执行此操作:

people = people.Where(p => p.Addresses.Any(a => a.City == "Something"))
               .ToList();