开发者

Using a custom IList obtained through NHibernate

开发者 https://www.devze.com 2022-12-27 01:26 出处:网络
I\'m trying to write a web page in .NET, using C# and NHibernate 2.1. The pertinent code looks like this:

I'm trying to write a web page in .NET, using C# and NHibernate 2.1.

The pertinent code looks like this:

var whatevervar = session.CreateSQLQuery("select thread_topic, post_time, user_display_name, user_signature, use开发者_Go百科r_avatar, post_topic, post_body from THREAD, [USER], POST, THREADPOST where THREADPOST.thread_id=" + id + " and THREADPOST.thread_id=THREAD.thread_id and [USER].user_id=POST.user_id and POST.post_id=THREADPOST.post_id ORDER BY post_time;").List();

(I have tried to use joins in HQL, but then fell back on this query, due to HQL's unreadability.) The problem is that I'm getting a result that is incompatible with a repeater. When I try this:

posts.DataSource = whatevervar.;
posts.DataBind();

...I get this:

DataBinding: 'System.Object[]' does not contain a property with the name 'user_avatar'.

In an earlier project, I used LINQ to SQL for this same purpose, and it looked like so:

var whatevervar = from threads in context.THREADs
                          join threadposts in context.THREADPOSTs
                            on threads.thread_id equals threadposts.thread_id
                          join posts1 in context.POSTs
                            on threadposts.post_id equals posts1.post_id
                          join users in context.USERs
                            on posts1.user_id equals users.user_id
                          orderby posts1.post_time
                          where threads.thread_id == int.Parse(id)
                          select new
                          {
                              threads.thread_topic,
                              posts1.post_time,
                              users.user_display_name,
                              users.user_signature,
                              users.user_avatar,
                              posts1.post_body,
                              posts1.post_topic
                          };

That worked, and now I want to do the same with NHibernate. Unfortunately, I don't know how to make the repeater recognize the fields of the result of the query.

Thanks in advance!


NHibernate can return non-managed entities from SQL queries.

Create a non-mapped class to hold the result of this query, and have NHibernate return a list of this type.

public class MyDTO
{
    public string thread_topic { get; set; }
    public DateTime post_time { get; set; }
    public string user_display_name { get; set; }
    public string user_signature { get; set; }
    public string user_avatar { get; set; }
    public string post_topic { get; set; }
    public string post_body { get; set; }
}

var whatevervar = session.CreateSQLQuery(@"select thread_topic, post_time, user_display_name, user_signature, user_avatar, post_topic, post_body
    from THREAD, [USER], POST, THREADPOST
    where THREADPOST.thread_id=" + id + " and THREADPOST.thread_id=THREAD.thread_id and [USER].user_id=POST.user_id and POST.post_id=THREADPOST.post_id
    ORDER BY post_time;")
.SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)))
.List();
0

精彩评论

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

关注公众号