ActiveRecord做多表查询得出的结果怎么处理?
1.先在Domain中建一个实体类,如Map
public class Map
{
private string _mapCreator;
private int _mapRecords;
public string MapCreator
{
get{return _mapCreator;}
set{_mapCreator=value;}
}
public int MapRecords
{
get{return _mapRecords;}
set{_mapRecords=value;}
}
public Map(string creator,int count)
{
_mapCreator=creator;
_mapRecords=count;
}
}
2.Domain中的实体类的实现
//这是一个实体类中的一个方法,通过Hql实现:
public IList FindGroupByCreator()
{
//string query="select new Map(DataInfo.Creator,count(*)) from DataInfo group by DataInfo.Creator";
string query="select di.Creator,count(*) from DataInfo di group by di.Creator";
IList list=this.ExecuteHql(query);
return list;
}
public IList ExecuteHql(string query)
{
this.queryString=query;
return (IList)Execute(new NHibernateDelegate(GetDataInfoCallback));
}
/// <summary>
/// 回调
/// </summary>
/// <param name="session"></param>
/// <param name="instance"></param>
/// <returns></returns>
private object GetDataInfoCallback(ISession session, object instance)
{
IQuery query=session.CreateQuery(this.queryString);
IList results=query.List();
return results;
}
3.//在web层作如下操作
private void BindAll()
{
DataInfo obj=new DataInfo();
IList list=obj.FindGroupByCreator();
Map[] mapArray=new Map[list.Count];
for(int i=0;i<list.Count;i++)
{
mapArray[i]=new Map("",0);
mapArray[i].MapCreator=(((object[])(list[i]))[0]).ToString();
mapArray[i].MapRecords=int.Parse((((object[])(list[i]))[1]).ToString());
}
resultList=mapArray;
DataSource=resultList;
dgLabour.DataBind();
}
就是这样。