문제

I have created a ExpandoResultTransformer, that would be used for converting query results to dto to be used in UI. It looks like this:

  public class ExpandoResultTransformer : IResultTransformer
    {
        public object TransformTuple(object[] tuple, string[] aliases)
        {
            dynamic toReturn = new ExpandoObject();
            for (int i = 0; i < aliases.Length; i++)
            {
                ((IDictionary<string, object>)toReturn)[aliases[i]] = tuple[i];
            }
            return toReturn;
        }

        public IList TransformList(IList collection)
        {
            return collection;
        }
    }

When I use it against a createSQLQuery it works perfectly

var contacts = _session.CreateSQLQuery("exec up_ClientContactsView @ClientId=:clientId")
                                   .SetParameter("clientId", request.ClientId)
                                   .SetResultTransformer(new ExpandoResultTransformer())
                                   .List<dynamic>();

However, when I tried using it against QueryOver, I run into trouble. There are no aliases sent in. So I tried specifying them like this

var periodTables = _session.QueryOver<PeriodTable>()
                           .Where(x => x.ClientPolicy.Id == request.ClientPolicyId)
                           .SelectList(list => list
                              .Select(i =>i.Id).WithAlias(()=>info.PeriodTableId) !! does not work as dynamic cannot be used in expression error
                               .Select(i => i.Id).WithAlias(()=>"PeriodTableId" !!! does not work either as it cannot find member error
                           .TransformUsing(new ExpandoResultTransformer())
                           .List<dynamic>();

Any ideas how to pass in aliases?

도움이 되었습니까?

해결책

You can use an anonymous type instance to act as a template object:

var template = new { PeriodTableId = 0 };

var periodTables = _session.QueryOver<PeriodTable>()
                       .Where(x => x.ClientPolicy.Id == request.ClientPolicyId)
                       .SelectList(list => list
                          .Select(i =>i.Id).WithAlias(()=>template.PeriodTableId) 
                          )
                       .TransformUsing(new ExpandoResultTransformer())
                       .List<dynamic>();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top