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!