nhibernate - Changing HQL to Linq? -
i have situation using named hql query, dal has changed, don't know how re-do named query in linq. have entity called holtertest, read in. need create workitem contain holtertest, in order that, need find of existing holtertests don't yet have workitem associated them.
here original hql named query:
<query name="get.holtertests.without.workitems" > <![cdata[ holtertest ht not exists (from workitem wi wi.holtertest.id = ht.id) , ht.recordingstartdatetime == null]]> </query>
here nhibernate mapping workitem entity:
<class name="workitem" table="workitems" where="" dynamic-update="true" dynamic-insert="true" > <id name="id" column="workitemid" type="int32" > <generator class="identity"/> </id> <property name="status" type="ansistring" /> <property name="iscompleted" /> <property name="isstarted" /> <property name="completeddatetime" type="datetime" /> <many-to-one name="holtertest" lazy="false" class="holtertest" column="holtertestid" unique="true" not-null="true" cascade="save-update"/> <bag name="worktasks" cascade="all-delete-orphan" inverse="true" lazy="false" > <key column="workitemid" on-delete="cascade" /> <one-to-many class="holtermanagementbackend.domain.models.worktask"/> </bag>
here mapping file holtertest object, have one-to-one mapping.
<class name="holtertest" table="holtertests" where="" dynamic-update="true" dynamic-insert="true"> <id name="id" column="holtertestid" type="int32" > <generator class="identity"/> </id> <property name="mayoclinicnumber" type="ansistring" /> <property name="holtertesttype" type="int32" /> <property name="lastname" type="ansistring" /> <property name="recordingstartdate" type="datetime" not-null="false" /> <property name="hookupapptdate" type="datetime" not-null="false" /> <property name="followupapptdate" type="datetime" not-null="false" /> <property name="room" type="ansistring" /> <property name="holterunitserial" type="ansistring" /> <property name="hookuptechlanid" type="ansistring" /> <property name="orderingmdlanid" type="ansistring" /> <property name="reviewingmdlanid" type="ansistring" /> <one-to-one name="workitem" lazy="false" class="workitem" property-ref="holtertest" cascade="save-update" /> <many-to-one name="location" class="location" column="locationid" not-found="ignore" fetch="join" lazy="false"/>
in rework of dal, have abstract class specification using linq:
namespace holtermanagementbackend.dal.contracts { public abstract class specification<t> { public abstract expression<func<t, bool>> matchingcriteria { get; } public t satisfyingelementfrom(iqueryable<t> candidates) { return satisfyingelementsfrom(candidates).single(); } public iqueryable<t> satisfyingelementsfrom(iqueryable<t> candidates) { return candidates.where(matchingcriteria).asqueryable(); } } }
this used in dao object's findall method:
/// <summary> /// finds objects match specification expression provided /// </summary> /// <param name="query">the expression search on</param> /// <returns>all objects match criteria</returns> [transaction( readonly = true)] public ilist<t> findall(specification<t> query) { return query.satisfyingelementsfrom(sessionfactory.getcurrentsession().query<t>()).tolist(); }
here simpler specification, holtertests region, hookupapptdate range.
public class getholtertesttohookupbyregionid : specification<holtertest> { private readonly int _days; private readonly int _regionid; public getholtertesttohookupbyregionid(int regionid, int numberofdays) { _days = numberofdays; _regionid = regionid; } public override expression<func<holtertest, bool>> matchingcriteria { { return x => x.hookupapptdate <= convert.todatetime(system.datetime.today.adddays(_days)) && x.location.region.id == _regionid && x.recordingstartdate == null; } } }
what don't know how write specification includes notexists , join. suggestions/ideas?
update: one-to-one, when holtertest objects recordingstartdate null, can loop through , find ones workitem null, when try put x.workitem == null query spec, don't back.
you give holtertest
collection property named workitems
, check doesn't contain anything:
x.recordingstartdate == null && !x.workitems.any()
Comments
Post a Comment