處理HQL複雜查詢當使用left outer join 聯合多個表查詢資料時,返回的不是一個對象,而是一個多維[][]數組。

來源:互聯網
上載者:User

/*
*Description:HQL中用left outer join來串連多個對象*Auther:天很藍_崇崇
*MSN:chongchong2008@msn.com
*Dates:2006-05-22
*Copyright:ChongChong2008 YiChang HuBei China
*/

當你用left outer join來串連多個對象的時候,注意返回的IList 或者是IEnumerable都不直接是一個實體物件,而是一個多維的數組,這一點一定要注意,否則你很難找到錯誤所在。

下面我來介紹一下:

Descriptions是WordEntity的一個IList類型的屬性,也就是WordEntity裡的Descriptions是一個一對多的東西

 public static IList GetWrod( string title )
        {

            SimpleQuery q = new SimpleQuery(
                typeof(WordEntity), typeof(typeof(IList)),
                //"from WordEntity word where word.Title like ?", //沒有加left outer join的話這個輸出就有內容
                @"from WordEntity word left outer join word.Descriptions description where word.Title like ? and word.DescriptionId in elements(word.Descriptions)", //用這個輸出就沒有資訊,可以資料表裡石油資訊的
                title
            );
            return (IList)ExecuteQuery(q);
}

返回的這個IList實際上是一個實體[][].顯然不符合我們的要求,我們需要的是一個集合對象

那麼如何把返回的這個二維數群組轉換為我們的對象集合呢,跟我來....

            IList wordList = WordEntity.GetWord("Web%");
            int maxIndex = wordList.Count * 2;
            Response.Write(wordList.Count.ToString() + "---" + wordList.GetType().ToString() + "</br>" ) ;

            WordEntity myA;
            ArrayList myAs = new ArrayList();
            DescriptionEntity myB;

            int ii = 1;
            foreach (object[] o in wordList)
            {
                myA = new WordEntity();
                Response.Write(o.ToString() + "</br>");

                foreach (object oo in o)
                {
                    Response.Write("第" + ii.ToString() + "個元素 Start" + "</br>");
                    if (ii % 2 != 0)
                    {
                        if (ii <= maxIndex)
                        {
                            Response.Write(((WordEntity)oo).WordTitle + "</br>");
                            myA.WordTitle = ((WordEntity)oo).WordTitle;
                        }

                    }
                    else
                    {
                        myB = new DescriptionEntity();
                        myB.DescriptionContent = ((DescriptionEntity)oo).DescriptionContent;
                        myA.Descriptions.Add(myB);

                        Response.Write(((DescriptionEntity)oo).DescriptionContent+ "</br>");
                        myAs.Add(myA);
                    }

                    Response.Write("第" + ii.ToString() + "個元素 End" + "</br>");
                    ii++;

                }               
            }

好了,下面就可以利用foreach來遍曆了~~~

            foreach (WordEntity we in myAs)
            {
                Response.Write(we.WordTitle + "</br>");
                foreach (DescriptionEntity d in we.Descriptions)
                {
                    Response.Write(d.DescriptionContent + "</br>");
                    Response.Write(d.DescriptionDate.ToString("yyyy-M-dd") + "</br>");
                }
                Response.Write("<p></p>");
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.