开发者

using HQL to concatenate strings from multiple rows

开发者 https://www.devze.com 2023-02-10 11:14 出处:网络
Consider a table 开发者_如何学Cholding names, with three rows: Peter Paul Mary using NHibernate HQL I want toretrieve all the names as a single string\"Peter, Paul, Mary\" to put it inside a single

Consider a table 开发者_如何学Cholding names, with three rows:

Peter Paul Mary

using NHibernate HQL I want to retrieve all the names as a single string "Peter, Paul, Mary" to put it inside a single DTO object field. is there a way to do this kind of concatenation?


No, there isn't.

It's not possible to do string column aggregation in SQL, except maybe by using specific RDBMS features.

Just bring all the names and concatenate them client-side,


It is too simple to implement this in C#, so it's not worth to try too hard to tweak NHibernate to do it (you may use functions or formula or ...)

class MyDto
{
  string Name1 { get; set; }
  string Name2 { get; set; }
  string Name3 { get; set; }

  string Names
  { 
    get 
    {
      return string.Format("{0}, {1}, {2}", Name1, Name2, Name3)
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号