【问题标题】:LINQ to NHibernate expressions on List<>List<> 上的 LINQ to NHibernate 表达式
【发布时间】:2011-02-14 17:28:13
【问题描述】:

我正在使用 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" 的人。

【问题讨论】:

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


    【解决方案1】:

    怎么样:

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

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

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

    【讨论】:

    • 感谢乔恩,它很有魅力。我需要处理我的 LINQ foo。我的“Jon Skeet 回答了我的问题”徽章在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多