c# - List<> 上的 LINQ to NHibernate 表达式

标签 c# linq nhibernate linq-to-nhibernate

我正在使用 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/

相关文章:

c# - WCF 服务和 POODLE 攻击

c# - NHibernate session 应该如何在 Nancy 中处理每个请求的 session ?

c# - 如何使用 nhibernate 通过递归自连接 SQL 获得最佳性能

c# - 使用 LINQ to Objects 在一个集合中查找与另一个集合不匹配的项目

c# - 将 't' 'f' 转换为 bool 值 "String was not recognized as a valid Boolean."时出现流利的 nhibernate 错误

c# - PHP的number_format在C#中的等效功能是什么?

c# - 将 DependencyProperty 转发到用户控件中包含的控件

c# - 如何找到 SpeechSynthesizer 所选语音的音频格式

c# - DbExpressionBinding 需要一个带有集合 ResultType 的输入表达式

c# - 使用 Linq to Entities 进行分组和多重排序