多表的查詢
關于查詢,這個東東,也困惑了我好常時間,對于對于單表,直接使用Hql寫查詢,然后使用Iquery.list()來獲得結果,
不過1.2里面不能直接這樣使用,需要用AddEntity給他格式一下結構.并且,我看幫助里面使用的都是ISQLQuery.
于是乎,對于單表,需要ISQLQuery.AddEntity(typeof(Class)).list()來的到結果.,對于多表,比如上面的兩個表datazj和userzj
Hql=select a.meid,a.dz,b.zname from datazj a,userzj b where a.meid=b.meid
可以寫成
1
ISQLQuery isq = session.CreateSQLQuery("select a.meid,a.dz,b.zname from datazj a,userzj b where a.meid=b.meid ");
2
3
isq.AddScalar("meid", NHibernateUtil.Int32);
4
isq.AddScalar("dz", NHibernateUtil.String);
5
isq.AddScalar("zname", NHibernateUtil.String);
6
IList il = isq.List();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
而得到的這個ilist我又要痛苦了,因為目前得到的這個結果不能直接綁定到DataSource里面去,綁上去后顯示的都是一些亂七八糟的東西.
于是又搜索Ilist to DataTable的方法,在搜索試驗很久后,突然靈感來了,然后寫了如下的轉換函數
1
public static DataTable IListToTable ( IList il )
2
{
3
try
4
{
5
DataTable dt = new DataTable();
6
if (il == null) return null;
7
if (il.Count <= 0) return null;
8
object[] te = (object[]) il[0];
9
for (int i = 0; i < te.Length; i++)
10
{
11
dt.Columns.Add("a" + i, te[i].GetType());
12
}
13
foreach (object[] a in il)
14
{
15
dt.Rows.Add(a);
16
}
17
return dt;
18
}catch
19
{
20
throw new ApplicationException("(#zjErr#) ZJFactory.DB.IListToTable");
21
}
22
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
不過經過試驗,好像如果是直接AddEntity得到的ilist,其結構就是一個完成的類,那么用我上面的函數,執行到
object[] a=(object[])ilist[i]
的時候就報錯了!不過此類的ilist可以直接綁定到數據控件!也不需要轉換了!