Processing hql complex queries when you use left outer join to join multiple tables to query data, the returned data is not an object, but an array of multidimensional.

Source: Internet
Author: User

/*
* Description: In hql, left Outer Join is used to connect multiple objects * auther: the sky is blue _ Chong
* MSN: chongchong2008@msn.com
* Dates: 2006-05-22
* Copyright: chongchong2008 Yichang Hubei China
*/

When you use left outer join to connect multiple objects, note that the returned ilist or ienumerable is not directly an object, but a multi-dimensional array. Pay attention to this in 1.1, otherwise, it is difficult to find the error.

I will introduce it as follows:

Descriptions is an ilist type attribute of wordentity, that is, descriptions in wordentity is a one-to-many thing.

Public static ilist getwrod (String title)
{

simplequery q = new simplequery (
typeof (wordentity), typeof (ilist),
// "from wordentity word where word. Title like? ", // If left Outer Join is not added, the output will have content
@" from wordentity word left Outer Join word. Descriptions description where word. Title like? And word. descriptionid in elements (word. descriptions) ", // with this output, there is no information. You can
title
of the petroleum information in the data table);
return (ilist) executequery (Q );
}

The returned ilist is actually an object [] []. Obviously it does not meet our requirements. What we need is a set object.

So how can we convert the returned two-dimensional array to our object set? Come with me ....

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 ("no." + II. tostring () + "element start" + "</BR> ");
If (II % 2! = 0)
{
If (II <= maxindex)
{
Response. Write (wordentity) Oo). wordtitle + "</BR> ");
Mya. wordtitle = (wordentity) Oo). wordtitle;
}

}< br> else
{< br> myb = new descriptionentity ();
myb. descriptioncontent = (descriptionentity) Oo ). descriptioncontent;
Mya. descriptions. add (myb);

Response. Write (descriptionentity) Oo). descriptioncontent + "</BR> ");
Myas. Add (Mya );
}

Response. Write ("no." + II. tostring () + "element end" + "</BR> ");
II ++;

}
}

Now, we can use foreach to traverse the process ~~~

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> ");
}

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.