Share NHibernate parameter in multiple queries - nhibernate

I have two queries, first query return top 10/20 recoords, second query return total record count from first query. Both queries need to use same filter condition.
How can I write filter condition and parameter used in filter condition in one place and use in both the queries.
Condition I can store in string variable and use in both the queries but how to share parameters?
I am using HQL

Check this similar Q & A: Nhibernate migrate ICriteria to QueryOver
There is a native support in NHiberante for row count. Let's have some query
// the QueryOver
var query = session.QueryOver<MyEntity>();
It could have any amount of where parts, projections... Now we just take its underlying criteria and use a transformer to create brand new criteria - out-of-box ready to get total rowcount
// GET A ROW COUNT query (ICriteria)
var rowCount = CriteriaTransformer.TransformToRowCount(query.UnderlyingCriteria);
Next step is to use FUTURE to get both queries in one round trip to DB
// ask for a list, but with a Future, to combine both in one SQL statement
var list = query
.Future<MyEntity>()
.ToList();
// execute the main and count query at once
var count = rowCount
.FutureValue<int>()
.Value;
// list is now in memory, ready to be used
var list = futureList
.ToList();

Related

WPF Comparing two datatables to find matching values

enter code hereI have two data tables one of the data tables connects to sql server and the other connects to oracle.
I am running query statements on both and those work perfectly find.
Now I need to write something that will compare the "UNIT_NO" in oracle to the "VehicleName" in sql. yes they are the same number.
Right now the Oracle table bring in 6 columns and the SQL brings in 4 columns
an example would be:
VehicleName, VehicleGroupName, UserDefinedColumn2, UserDefinedColumn3
Unit_No, Unit_ID, Using_Dept, Status, Using_Dept_Desc,
I want my code to find the matching number from Unit_NO and VehicleName and display all the above information all in one row. I was thinking linq but I cant get it to display correctly
This code combines the columns from both tables but pulls but does not add the any data in the rows any suggest or fixes
private void GetSQLOraclelinqData()
{
var TstarData = GetTrackstarTruckData();
var M5Data = GetM5Data();
DataTable ComTable = new DataTable();
foreach (DataColumn OraColumn in M5Data.Columns)
{
ComTable.Columns.Add(OraColumn.ColumnName, OraColumn.DataType);
}
foreach (DataColumn SQLColumn in TstarData.Columns)
{
if (SQLColumn.ColumnName == "VehicleName")
ComTable.Columns.Add(SQLColumn.ColumnName + 2, SQLColumn.DataType);
else
ComTable.Columns.Add(SQLColumn.ColumnName, SQLColumn.DataType);
}
var results = TstarData.AsEnumerable().Join(M5Data.AsEnumerable(),
a => a.Field<String>("VehicleName"),
b => b.Field<String>("Unit_NO"),
(a, b) =>
{
DataRow row = ComTable.NewRow();
row.ItemArray = a.ItemArray.Concat(b.ItemArray).ToArray();
ComTable.Rows.Add(row);
return row;
});
SQLDataTable.ItemsSource = ComTable.DefaultView;
}
I would do it using two nested for loops.
The outer for loop would iterate through each row in the SQL DataTable.
The inner loop would iterate through each row in the Oracle DataTable, and if there is a match, it would store the match somewhere (perhaps in a list).
Optional Hints
Assuming that each number plate only occurs once, we could optimise this code by breaking out of the inner loop as soon as we get a match.
We cannot rely on the rows coming back in the same order. Thus, we cannot naively compare row 1 from SQL against row 1 from Oracle.
my code is based off grabbing columns that are not populated yet in the datagrid I call two datatable on the code behind to populate the table and also us excel sheets. If anyone needs this information i can help.
I connect to SQL, Oracle and load excel sheets to make a comparison on data

SQL select Count with where very slow

I use a count() function many times in my program:
var housewith2floor = from qry in houses
where qry.floor == 2
select qry;
var counthousewith2floor = housewith2floor.count();
var housecolorwhite = from qry in house
where qry.color == "white"
select qry;
var countwhotehouse = housecolorwhite.count();
Each count method takes a lot of time to be executed. The database has 2 millions rows of data. I already put an unclustered index for floor column and color column, but the count still takes too long. Is there any other way I can make my count run much faster?
It's not so much the count that takes time. The initial statement won't actually execute until it needs to (called deferred execution). So it's the query generated by
var housewith2floor = from qry in houses
where qry.floor == 2
select qry;
that takes the time.
Edit to remove statment about indexes as I see you have already created them.
Are there any tables which reference or are referenced by "houses", and do they use Lazy Loading?

Datatable Compute Method filter on row number

I use a query which fetches say 50 records and passes it to a datatable. This record is then displayed in a tabular format. The display has pagination used displaying 10 records at a time. There is a facility to move to next or previous set of record or move forward or backwards by 1 record.
I have to find Min and Max of a column for the set of record currently visible. I am planning to use Compute method but I am not sure if it allows filtering on anything other than the columns in datatable.
Do I have to include row number in my query or is there a better solution (something along the line mentioned below)?
CType(dtLineup.Compute("Min(ArrivalDate)", dt.row(2) to dt.row(12)), Date)
There is nothing like your pseudo code in MSDN on DataColumn.Expression. You could include a row number in your query, as you said, but an alternative is to add a row number column to your data table and use that in the filter expression.
DataColumn col = new DataColumn("rownumber", typeof(int));
col.AutoIncrement = true;
col.AutoIncrementSeed = 1;
datatable.Columns.Add(col);
Another alternative could be to do paging by linq (Skip-Take) and compute the aggregate function over the returned rows. But that may be a major departure of your current application structure.

Total count for a limited result

So i implemented the paging for dojo.store.jsonRest to use as store in the dojox.grid.DataGrid. In the server im using Symfony 2 and as ORM Doctrine, im new to this two frameworks.
For Dojo jsonRest the response of the server must have a header Content-Range containing the result offset, limit and the total number of records (without the limit).
So for a response with a Content-Range: items 0-24/66 header, if the user where to scroll the grid records to the 24 row, it will make a async request with Range: 24-66 header, then the response header should have a Content-Range: items 24-66/66. This is done so Dojo can know how many request it can make for the paginated data and the records range for the presented and subsequent request.
So my problem is that to get the total number of records without the limit, i had to make a COUNT query using the same query that has the offset and limit. I don't like this.
I want to know if there is a way i can get the total count and the limited result without making two queries.
public function getByTextCount($text)
{
$dql = "SELECT COUNT(s.id) FROM Bundle:Something s WHERE s.text LIKE :text";
$query = $this->getEntityManager()->createQuery($dql);
$query->setParameter('text', '%'.$text.'%');
return $query->getSingleScalarResult();
}
-
public function getByText($text, $offset=0, $limit=24)
{
$dql = "SELECT r FROM Bundle:Something s WHERE s.text LIKE :text";
$query = $this->getEntityManager()->createQuery($dql);
$query->setParameter('text', '%'.$text.'%');
$query->setFirstResult($offset);
$query->setMaxResults($limit);
return $query->getArrayResult();
}
If you're using MySQL, you can do a SELECT FOUND_ROWS().
From the documentation.
A SELECT statement may include a LIMIT clause to restrict the number
of rows the server returns to the client. In some cases, it is
desirable to know how many rows the statement would have returned
without the LIMIT, but without running the statement again. To obtain
this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT
statement, and then invoke FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
If you want to use Doctrine only (i.e. to avoid vendor-specific SQL) you can always reset part of the query after you have selected the entities:
// $qb is a Doctrine Query Builder
// $query is the actual DQL query returned from $qb->getQuery()
// and then updated with the ->setFirstResult(OFFSET) and ->setMaxResults(LIMIT)
// Get the entities as an array ready for JSON serialization
$entities = $query->getArrayResult();
// Reset the query and get the total records ready for the Range header
// 'e' in count(e) is the alias for the entity specified in the Query Builder
$count = $qb->resetDQLPart('orderBy')
->select('COUNT(e)')
->getQuery()
->getSingleScalarResult();

Field's value of native query in JPA

How to get value of some fields in a native query (JPA)?
For example I want to get name and age of customer table:
Query q = em.createNativeQuery("SELECT name,age FROM customer WHERE id=...");
Note: I don't want to map results to entities. I just want to get the value of the field.
Thanks
A native query with multiple select expressions returns an Object[] (or List<Object[]>). From the specification:
3.6.1 Query Interface
...
The elements of the result of a Java
Persistence query whose SELECT clause
consists of more than one select
expression are of type Object[]. If
the SELECT clause consists of only one
select expression, the elements of the
query result are of type Object. When
native SQL queries are used, the SQL
result set mapping (see section
3.6.6), determines how many items (entities, scalar values, etc.) are
returned. If multiple items are
returned, the elements of the query
result are of type Object[]. If only a
single item is returned as a result of
the SQL result set mapping or if a
result class is specified, the
elements of the query result are of
type Object.
So, to get the name and age in your example, you'd have to do something like this:
Query q = em.createNativeQuery("SELECT name,age FROM customer WHERE id = ?1");
q.setParameter(1, customerId);
Object[] result = (Object[])q.getSingleResult();
String name = result[0];
int age = result[1];
References
JPA 1.0 specification
Section 3.6.1 "Query Interface"
Section 3.6.6 "SQL Queries"
Depends on the JPA implementation. Hibernate does it different than castor, for example.
Here's a example how it would work in Hibernate:
Query query = session.createSQLQuery(
"select s.stock_code from stock s where s.stock_code = :stockCode")
.setParameter("stockCode", "7277");
List result = query.list();
I don't think that it is possible to get a plane value such as an integer... but this one should come very close to it.