List<> 上的 LINQ to NHibernate 表达式

发布于 2024-10-17 11:52:42 字数 643 浏览 2 评论 0 原文

我正在使用 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 == 的人“某物”。

I'm using LINQ to NHibernate, and have a model that looks something like this (simplified):

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; }
}

I can perform the following LINQ to NHib query:

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

But I'm stuck trying to return all the people who have an address with City == "Something".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-10-24 11:52:42

怎么样:

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

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

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

How about:

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

That's assuming you want the query to still be performed in the database. If you just want to do it within the List<Person> already returned:

people = people.Where(p => p.Addresses.Any(a => a.City == "Something"))
               .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文