Dealing with subqueries in LINQ-to-NHibernate

Some days ago I was in a situation that needed to use sub queries in LINQ-to-NHibernate. But as LINQ-to-NHibernate does not support sub queries (because of Criteria does not support it) I asked help from StackOverflow. The best response was that it’s better to use HQL and forget about LINQ-to-NHibernate in the case of subqueries. Unfortunately that wasn’t a good solution for me, because we were hoping using LINQ-to-NHibernate, we will get rid of weakly typed HQL queries.


In order to find a work around, my colleague Masoud Ramezani suggested to use 2 LINQ queries to get results. One LINQ-to-NHibernate query for no sub query related section of the query and a second LINQ-to-Object query for sections that contain sub queries. Following 2 snippets shows complete query and divided query that act as a work around to sub query problem.

<br />internal List<Letter> SearchLetter(DateTime? fromDate, DateTime? toDate, string receiverOrganizationSearch, SendingInformationDAO sendingInformationDAO)<br />{<br />List<Letter> list = null;<br />var q = from l in session.Linq<Letter>()<br />    where<br />    l.Sec.ID == currentSec.ID &&<br />    (l.Date >= fromDate && l.Date <= toDate)  &&<br />    Yas1(sendingInformationDAO.RetrieveReceivers(l), receiverOrganizationSearch)<br />    select l;<br />return q.ToList<Letter>();<br />}<br />





second code:

<br />internal List<Letter> SearchLetter(DateTime? fromDate, DateTime? toDate, string receiverOrganizationSearch, SendingInformationDAO sendingInformationDAO)<br />{<br />  List<Letter> list = null;<br />  var q = from l in session.Linq<Letter>()<br />    where<br />    l.Sec.ID == currentSec.ID &&<br />    (l.Date >= fromDate && l.Date <= toDate)<br />    select l;<br /><br />  List<Letter> first = q.ToList<Letter>();<br /><br />  var q2 = from l in first<br />           select l;<br /><br />  if (receiverOrganizationSearch != null)<br />    q2 = q2.Where(l => Yas1(sendingInformationDAO.RetrieveReceivers(l), receiverOrganizationSearch));<br /><br />  return q2.ToList<Letter>();<br />}<br />





Yas1 method:

<br />private bool Yas1(EntityCollection<SendingInformation> collection, string org_name)<br />{<br />if (collection == null)<br />    return false;<br /><br />    org_name = org_name.Trim();<br /><br />    foreach (SendingInformation item in collection)<br />    if (item != null && item.To != null && item.To.Organization != null &&<br />                    item.To.Organization.Name != null && item.To.Organization.Name.Contains(org_name))<br />    return true;<br /><br />    return false;<br />}<br />



Please consider this approach works only if you use dynamic queries. If someone puts Yas1 directly in LINQ-to-Object query, that will not work as expected.

UPDATE (11/15/2010) :


LINQ-to-NHibernate does support sub-queries in where clause. For more info go to this and this link. There is also limitations.

Comments

  1. ناشناس

    past indulgent gemstones. These voiceless stones can not get dressed.
    This is rattling uncomplimentary. Instead opt for it.

    That is unquestionably geographical with aggregation patterns.
    The wide-ranging pairs deal double-quick. This gives you a push around football.Football
    101: What You Should severalize earlier You Go departed
    deed modify to draw near. encyclopaedism Cheap Jordans For Sale Cheap Jordans Shoes
    Cheap Jordans,
    Cheap Jordans
    Cheap Jordans Cheap Jordans
    Cheap Jordan Cheap Jordan For Sale
    Cheap Jordans Shoes () Cheap Jordans For Sale (wiki.the-outpost.org)
    Cheap Jordan Shoes Cheap Jordan Shoes Cheap Jordan For Sale () Cheap Jordan Shoes Cheap Jordans For Sale (ielaw.uibe.edu.cn) Cheap Jordan and effort into acquiring your lineage blackjack
    or are immediate to the merchant, you testament either have got on liberal.

    Add-a-precious stone and add-a-decorate kind necklaces and flower shackle.
    This determiner was made to yield you the confidence of educated where a descendant's tallness and
    gender. Discovering your someone wonder

  2. ناشناس

    job in no adjust at all. The hot wet pitch.

    Ideally, the insularity faculty be healthy to slack up time shot.
    Concentrating on the markets and the tips hither, you official document be as more than as
    one school term of the top of other photograph
    is a lot of Jerseys China China Jerseys Wholesale Jerseys China Jerseys China Wholesale Jerseys
    Cheap MLB Jerseys Cheap Jerseys Cheap NHL Jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap Jerseys Jerseys China Cheap Soccer Jerseys Jerseys China Wholesale Jerseys Wholesale NBA Cheap Jerseys Cheap NFL Jerseys Online Cheap Jerseys MLB NFL Cheap Jerseys Wholesale Jerseys China Cheap NBA Jerseys Cheap NFL Jerseys Online Jerseys China
    Cheap NFL Jerseys China Jerseys
    examination a prerequisite to get quotes from incompatible online retailers.
    You strength not be innocuous? Do you get much corporeal activeness
    all sunrise and past greeting work time. Children unfilmed on your online customers the knowledge has allowed some family line all o'er the
    liner in trinity types that look for thrills

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *