Activerecord multi-Table query, return datatable

Source: Internet
Author: User

1. hql returns ilist and combines it into a able.

String hql = "from item I left join I. Products P left join P. failuremodes F"
+ "Where P. productname =? ";
Castle. activerecord. Queries. simplequery query =
New Castle. activerecord. Queries. simplequery (typeof (item), typeof (ilist), hql, "AA ");
Ilist item = (ilist) Castle. activerecord. activerecordbase. executequery (query );

//// The specific field to be queried, which must be passed in dynamically
Private datatable maketable (ilist fields, ilist item)
{

Datatable table = new datatable ("data ");

Datacolumn column = NULL;

Foreach (queryfield field in fields)
{
Column = new datacolumn ();
Column. datatype = system. type. GetType ("system. String ");
Column. Caption = field. displaytext;
Column. columnname = field. displaytext;
Table. Columns. Add (column );
}

Datarow row;
Foreach (object [] ol in item)
{
Row = table. newrow ();

Int J = 0;
Foreach (Object o in Ol)
{
Row [Table. Columns [J] = O;
J ++;
}
Table. Rows. Add (ROW );
}
Return table;
}


 

2. Return datatable using SQL statements

String ssql = "select I. itemname, P. productname, F. failurename"
+ "From item I left join product P on I. Itemid = P. Itemid"
+ "Left join failuremode F on p. productid = f. productid"
+ "Where P. productname = 'A '";

Castle. activerecord. framework. isessionfactoryholder sessionholder =
Castle. activerecord. activerecordmediator. getsessionfactoryholder ();
nhib.pdf. isession session = sessionholder. createsession (typeof (item);

try
{< br> idbcommand command = session. connection. createcommand ();
command. commandtext = ssql;
idatareader RDR = command. executereader ();
datatable dt = new datatable ();
DT. load (RDR, loadoption. upsert);

Datagridview1.datasource = DT;

}
Finally
{
Sessionholder. releasesession (session );
}

3. Public static ilist <t> getdatasource <t> (Project curproject)
{

Try
{
Type T = typeof (t );

If (t = NULL) return NULL;

Parameterexpression Param = expression. parameter (T, "TT ");
// C. City = "London"
Expression left = expression. Property (Param, T. getproperty ("project "));
Expression right = expression. Constant (curproject );
Expression Filter = expression. Equal (left, right );
Expression Pred = expression. Lambda (filter, Param );
// Where (C => C. City = "London ")

Iqueryable <t> query = from TT in activerecordlinq. asqueryable <t> ()
Select tt;

Expression expr = expression. Call (
Typeof (queryable ),
"Where ",
New Type [] {t },
Expression. Constant (query ),
PRED
);

VaR ls = query. tolist <t> (). asqueryable <t> (). provider. createquery <t> (expr );

Return ls. tolist <t> ();

}
Catch
{
Return NULL;
}

}

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.