NHibernate COUNT (*) Statistics questions

Source: Internet
Author: User

NHibernate This framework has been used for more than a year, relative to a great advantage, you can save a lot of time to write SQL.

But if you want to use it to do statistics, then a little sorry, can only be written manually. It's built-in stuff that's hard to meet your needs.

That's the problem I'm having. I need to count the number of each group in a table that is grouped according to a field. This SQL is simple

The table name is replaced with the object name, and the field is described with the corresponding attribute, which has been tossing for two hours. I've never used hql before. The first time.

Don't take offense if you are a master. I have no particular insight into this, purely a personal point of view. If it helps you to see it, please help to recommend it.

Don't blame it on the spray. Daniel skipped it. This is a small problem.

I'll stick to the core code below:

list<rockmessage> list = new list<rockmessage> ();

Nhibernate.isession session = Sessionfactory.opensession ();

String sql = @ "Select COUNT (Wechatid) as Todaycount, wechatid,visitnum,inputdate from Rockmessage";

if (!string. IsNullOrEmpty (IDS))

{

SQL + = "WHERE Wechatid in (" + IDs + ")";

}

if (today)

{

SQL + = "and inputdate like '%" + DateTime.Now.ToString ("yyyy-mm-dd") + "% '";

}

SQL + = "GROUP by Wechatid ORDER by COUNT (Wechatid) DESC";

IQuery q = Session. CreateQuery (SQL);

ilist<rockmessage> RK = q.list<rockmessage> ();

ilist<object> obj = q.list<object> ();

ilist<object[]> result = q.list<object[]> ();

for (int i = 0; I < result. Count; i++)

{

Rockmessage rm = new Rockmessage ();

Rm.todaycount = Convert.ToInt32 (result[i][0]);

Rm.wechatid = convert.tostring (result[i][1]);

List. ADD (RM);

}

List = (list<rockmessage>) RK;

return list;

Where I made a few mistakes when writing SQL,

1. I give statistics count (*) to alias no use as this time when the query will be error

2.Order by when using aliases will be an error. This has to be remembered.

3. There is also a conversion to list< your type > This time cannot be directly queried the conversion is to use an object array to do intermediate media

This is very simple, I hope you can help to see it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.