NHibernate - QueryOver Child rows as comma separated values

☆樱花仙子☆ 提交于 2019-12-13 15:53:06

问题


I have to write a query where for each vendor I have to read vendor's contact numbers from VendorContact but instead in rows, one row for each unique vendor and one columns for each phone number as comma separated string in main result. This can be done easily in plain SQL but I have to do it in QueryOver. I cannot declare variables in QueryOver SQL.

var vendorvar = Session.QueryOver<Vendor>(() => V)
    .Left.JoinQueryOver(() => v.ContactNumbers, () => VendorContact)
    .SelectList(list => lst
       .Select(() => v.Name)
       .Select(() => VendorContact.PhoneNumber))
    .TransformUsing(Transformers.AliasToBean<VendorModel>())
    .List<VendorModel>();

There could be more phone numbers for one vendor, so I need like

Vendor         Contacts
------         -----------------------------------------
V0001          905-2343444,416-4545454,647-8484834
V0002          905-2356219,416-4111111,647-8000004

Thanks


回答1:


In general, if that is really so easy that way:

...This can be done easily in plain sql but...

just use that SQL statement in the raw query. There is some DTO for results

public class MyDto
{
    public virtual string FirstAlias { get; set; } // set name and type you need
    public virtual string SecondAlias { get; set; }
}

And here we do query, transform and list:

// raw SQL query
var query = session
    .CreateSQLQuery("Select " +
                    " SomColumn AS FirstAlias, " +
                    " SomComputedColumn AS SecondAlias " +
                    " FROM mySchema.MyTable" +
                    " join, where, order by..... "
                   );

// here we set transformer (check the aliases)
query.SetResultTransformer(Transformers.AliasToBean<MyDto>());

// and there is nice C# result
var list = query.List<MyDto>();


来源:https://stackoverflow.com/questions/32730333/nhibernate-queryover-child-rows-as-comma-separated-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!