Search code examples
c#.netnhibernate

How do I convert the query I use join in Nhibernate to my model?


   var query = Session.QueryOver(() => districtalias).Left.JoinAlias(x => x.DfSystemCityId, () => cityalias).Where(y => y.DistrictName == name)
            .TransformUsing(Transformers.AliasToBean<GetDfSystemCityDistrict>()).
            Select(y => y.DistrictName)
            .Future<GetDfSystemCityDistrict>();


        IList<GetDfSystemCityDistrict> model = query.ToList();
        return model;

I get an error when I want to list the query and cast it in the model.

Screenshot of the actual error

Ceyhan is my data, GetDfSystemCityDistrict model i want to cast


Solution

  • The select must be before the transformer. Also took the guess that the alias should be in the Select als well.

    var model = Session.QueryOver<District>()
        .Where(x => x.DistrictName == name)
        .Left.JoinAlias((x => x.DfSystemCityId, () => cityalias)
        .SelectList(l => l
            .Select(x => x.DistrictName)
            .Select(() => cityalias.Name)
        )
        .TransformUsing(Transformers.AliasToBean<GetDfSystemCityDistrict>())
        .List<GetDfSystemCityDistrict>();