我正在使用 LINQ to 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"的人。
最佳答案
怎么样:
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();
关于c# - List<> 上的 LINQ to NHibernate 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995190/