Fluent NHibernate HQL 将多个类类型分配给多个表选择查询
我知道如何将类类型分配给从单个表检索的数据,如下所示:
HQL 查询:
select s from Employee e join e.Store s where s.Id = 1
代码:
var stores = session.CreateQuery(hql).List<Store>();
foreach (var store in stores)
{
Console.WriteLine(store.Name);
foreach (var product in store.Products)
{
Console.WriteLine(" " + product.Name);
}
}
但是我们如何将多个类类型分配给多个表连接查询?目前我无法指定任何类类型,因为数据来自多个表。
HQL查询:
select distinct s.Name,p.Name,p.Price,p.Location.Aisle,p.Location.Shelf from Store s join s.Products p where s.Id = 1
代码:
var rows = session.CreateQuery(hql).List(); // Using List() instead of List<T>()
for (int i = 0; i < rows.Count; i++)
{
IList cols = (IList)rows[i];
for (int j = 0; j < cols.Count; j++)
{
Console.Write(cols[j] + " ");
}
Console.WriteLine("");
}
I know how to assign class type to the data retrieved from single table like this:
HQL query:
select s from Employee e join e.Store s where s.Id = 1
Code:
var stores = session.CreateQuery(hql).List<Store>();
foreach (var store in stores)
{
Console.WriteLine(store.Name);
foreach (var product in store.Products)
{
Console.WriteLine(" " + product.Name);
}
}
But how can we assign multiple class types to multiple tables join query? Currently I'm unable to specify any class type as data is coming from multiple tables.
HQL query:
select distinct s.Name,p.Name,p.Price,p.Location.Aisle,p.Location.Shelf from Store s join s.Products p where s.Id = 1
Code:
var rows = session.CreateQuery(hql).List(); // Using List() instead of List<T>()
for (int i = 0; i < rows.Count; i++)
{
IList cols = (IList)rows[i];
for (int j = 0; j < cols.Count; j++)
{
Console.Write(cols[j] + " ");
}
Console.WriteLine("");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定一个变压器。一种选择是指定 EntityMap 转换器。
这将返回一个字典列表,其中包含基于您的查询的名称值对。或者,您可以创建一个映射到您的查询的 POCO 对象
然后指定不同的转换器
请记住,您需要为两种方法指定别名
You need to specify a transformer. One option is to specify an EntityMap transformer.
This will return a list of dictionaries that will contain name value pairs based on your query. Alternatively, you can create a POCO object that maps to your query
Then specify a different transformer
Keep in mind you'll need to specify aliases for both approaches