2011-02-14 30 views
2

我使用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到NHib查詢:

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

但我卡住試圖返回所有與城市==「東西」有地址的人。

回答

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(); 
+2

感謝喬恩,它的工作魅力。我需要在我的LINQ foo上工作。我的問題在哪裏,「我的問題是由Jon Skeet回答的」徽章? – 2011-02-14 17:55:48