Subqueries with QueryOver(带有 QueryOver 的子查询)
问题描述
我在使用带有 queryover 的子查询时遇到问题.
I have a issue in using subquery with queryover.
这就是我所拥有的
var address = QueryOver.Of<Address>()
.Where(x => x.City.IsLike("%" + city + "%")).Select(x => x.Person.Id);
var result = Session.QueryOver<Person>()
.Where(x => x.Type.IsLike(type + "%"))
.And(x => x.Name.IsLike("%" + name + "%"))
.WithSubquery.WhereExists(address);
我有一个 Person 表,一个人有多个地址.
I have a table for Person and a person has multiple addreses.
所以人ID、名称、类型
So Person id, name, type
和地址将有PersonId 和城市等.
and Address will have PersonId and city etc.
所以想通过姓名和类型以及地址表中的城市来搜索一个人
So want to search a person by name and type as well as City which is in Address table
推荐答案
试试这样的:
Address address = null;
Person person = null;
var addressSubQuery = QueryOver.Of<Address>(() => address)
.Where(Restrictions.EqProperty(Projections.Property(() => address.Person.Id), Projections.Property(() => person.Id)))
.Where(() => address.City.IsLike("%" + city + "%"));
var result = Session.QueryOver<Person>(() => person)
.Where(x => x.Type.IsLike(type + "%"))
.And(x => x.Name.IsLike("%" + name + "%"))
.WithSubquery.WhereExists(addressSubQuery);
您需要使用 QueryOver 的别名版本.通过这种方式,您可以从其他查询中引用 Person 元素,这些查询最终将链接到您的主查询中.
You need to use QueryOver's version of aliasing. This way you can reference the Person element from other queries which you will eventually link into your main query.
这与执行以下操作相同
Select * from Person
Where
Type like '%foo%'
and Name like '%bar%'
and exists ( select Id from Address
where
Address.PersonId = Person.Id
and Address.City like '%bar%' )
这篇关于带有 QueryOver 的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 QueryOver 的子查询


基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01