简体   繁体   中英

deferred execution of where in sql

i have an editor where people can create custom sql-queries. those queries are NHibernate.IQuery objects.

in our project we are using NHibernate.Linq.LinqExtensionMethods.Query to get an IQueryable object which can be used to apply filters that are executed deferred (everything on the DB).

now i want to create another filter that is based on the custom sql-queries. what i want is something like this:

queryable.Where(x=> <sql-query contains x>)

the only thing i can do right now is execute the sql-query beforehand and then filter the queryable with the resulting list of elements.

IList<T> elements = query.List<T>();
queryable.Where(x => elements.Contains(x)).ToList();

the problem with that approach is, that the list of elements can be huge. if its possible, i would like to perform the whole statement directly on the database, so that i dont have to transfer all objects to my application, then send all objects back to the database as the filter-parameters...

edit: the first query (the one yielding the list of elements to check for in the second query) is constructed from a plain sql-string as follows:

ISQLQuery sqlQuery = CurrentSession.CreateSQLQuery(myQueryString);//create query
sqlQuery.AddEntity(typeof (T)); //set result type
IQuery query = sqlQuery.SetReadOnly(true);

If you have id's, then you should select them in your query , and compare it to the corresponding id column in the second query.

var elementIdsToUseInContain = query
    .Select(x => x.YourIdProperty);
var result = queryable
    .Where(x => elementIdsToUseInContain.Contains(x.YourIdPropertyToCompareTo))
    .ToList();

Note: I haven't tested it, but it is fairly standard Linq. It depends on if NHibernate supports it, which i do not know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM