Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way is to project what you do want first then serialise the result into a DTO. </p> <p>However by doing this you run into an issue where you are creating DTO's with lots of setters/getters. Wouldn't it be easier to create a simple anonymous type and serialise that instead? I have written a <a href="http://puredotnetcoder.blogspot.co.uk/2012/04/setresulttransformer-into-anonymous.html" rel="nofollow">blog post</a> that explains this. </p> <pre><code>//first create our anonymous type DTO var dto = new { Id = 0L, Source = string.Empty, Destination = string.Empty, Is301 = false }; //notice the ListAs(dto) extension method var model = Session.QueryOver&lt;CmsRedirect&gt;() .SelectList(s =&gt; s .Select(x =&gt; x.Id).WithAlias(() =&gt; dto.Id) .Select(x =&gt; x.Source).WithAlias(() =&gt; dto.Source) .Select(x =&gt; x.Destination).WithAlias(() =&gt; dto.Destination) .Select(x =&gt; x.Do301).WithAlias(() =&gt; dto.Is301) ) .Take(take).Skip(page * pageSize) .ListAs(dto); return Json(new { Total = total, List = model }, JsonRequestBehavior.AllowGet); </code></pre> <p>ListAs is a simple(ish) extension method. (credit goes to Filip Kinský)</p> <pre><code>public static class NHibernateExtensions { public static IList&lt;TRes&gt; ListAs&lt;TRes&gt;( this IQueryOver qry, TRes resultByExample) { var ctor = typeof(TRes).GetConstructors().First(); return qry.UnderlyingCriteria .SetResultTransformer( Transformers.AliasToBeanConstructor( (ConstructorInfo) ctor) ).List&lt;TRes&gt;(); } } </code></pre> <p>But be warned in my example I do not serialise children collections so you may need to handle this manually yourself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload