Fluent NHibernate HQL 将多个类类型分配给多个表选择查询

发布于 2024-10-30 12:53:08 字数 947 浏览 2 评论 0原文

我知道如何将类类型分配给从单个表检索的数据,如下所示:

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 技术交流群。

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

发布评论

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

评论(1

茶花眉 2024-11-06 12:53:08

您需要指定一个变压器。一种选择是指定 EntityMap 转换器。

session.CreateQuery.SetTransformer(Transformers.AliasToEntityMap)

这将返回一个字典列表,其中包含基于您的查询的名称值对。或者,您可以创建一个映射到您的查询的 POCO 对象

public class MyProjection
{
   public string SName { get; set; }
   public string PName { get; set; }
   public float Price { get; set; }
   public string Aisle { get; set; }
   public string Shelf { get; set; }
}

然后指定不同的转换器

session.CreateQuery.SetTransformer(Transformers.AliasToBean<MyProjection>())
      .List<MyProjection>();

请记住,您需要为两种方法指定别名

select distinct s.Name as SName,p.Name as PName,p.Price as Price,
     p.Location.Aisle as Aisle, p.Location.Shelf as Shelf...

You need to specify a transformer. One option is to specify an EntityMap transformer.

session.CreateQuery.SetTransformer(Transformers.AliasToEntityMap)

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

public class MyProjection
{
   public string SName { get; set; }
   public string PName { get; set; }
   public float Price { get; set; }
   public string Aisle { get; set; }
   public string Shelf { get; set; }
}

Then specify a different transformer

session.CreateQuery.SetTransformer(Transformers.AliasToBean<MyProjection>())
      .List<MyProjection>();

Keep in mind you'll need to specify aliases for both approaches

select distinct s.Name as SName,p.Name as PName,p.Price as Price,
     p.Location.Aisle as Aisle, p.Location.Shelf as Shelf...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文